查看: 1267|回复: 0
打印 上一主题 下一主题

Unity3D 第三课如何用滚轮放大缩小你的游戏

[复制链接]

435

主题

2

听众

6371

积分

高级设计师

Rank: 6Rank: 6

纳金币
6372
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-8-29 10:05:01 |只看该作者 |倒序浏览
首先要以你当前控制的角色为中心,
我们需要以下变量:

//最小缩小距离
public float theDistance = -10f;
//最大缩小距离
public float MaxDistance = -10f;
//缩放速度
public float ScrollKeySpeed = 100.0f;

早Update () 初始化
// 滚轮设置 相机与人物的距离.
if(Input.GetAxis("Mouse ScrollWheel") != 0)
{
theDistance = theDistance + Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ScrollKeySpeed;
}
// 鼠标滚轮滚动
if(theDistance>0)
theDistance = 0;
if(theDistance < MaxDistance)
theDistance = MaxDistance;

下面贴出全部代码
using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {

public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;

public float minimumX = -360F;
public float maximumX = 360F;

public float minimumY = -85F;
public float maximumY = 4F;

public float rotationY = 0F;

public GameObject target;

public float theDistance = -10f;
public float MaxDistance = -10f;
public float ScrollKeySpeed = 100.0f;
void Update ()
{
target = GameObject.Find("layer");
// 滚轮设置 相机与人物的距离.
if(Input.GetAxis("Mouse ScrollWheel") != 0)
{
theDistance = theDistance + Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ScrollKeySpeed;
}
// 鼠标中间滚动得到的值是不确定的,不会正好就是0,或 -10,当大于0时就设距离为0,小于MaxDistance就设置为MaxDistance
if(theDistance>0)
theDistance = 0;
if(theDistance < MaxDistance)
theDistance = MaxDistance;
if(Input.GetMouseButton(1))
{
transform.position = target.transform.position;
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
SetDistance();
}
else
{
transform.position = target.transform.position;
SetDistance();
}
}

void Start ()
{
if (rigidbody)
{
rigidbody.freezeRotation = ***e;
transform.position = target.transform.position;
}
}

//设置相机与人物之间的距离
void SetDistance()
{
transform.Translate(Vector3.forward * theDistance);
}
}
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-17 23:58 , Processed in 0.404371 second(s), 31 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部