找回密码
 立即注册
查看: 6903|回复: 80

[脚本] 用鼠标滚轮实现场景放大缩小

[复制链接]
发表于 2013-6-13 09:04 | 显示全部楼层 |阅读模式
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("new_offer");
// 滚轮设置 相机与人物的距离.
    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 = true;
        transform.position = target.transform.position;
    }
}

//设置相机与人物之间的距离
void SetDistance()
{
    transform.Translate(Vector3.forward * theDistance);
}

请问,运行以后发现,场景只是上下移动,并不实现放大缩小,这是为什么?

发表于 2017-4-13 18:31 | 显示全部楼层
很不错
发表于 2017-4-13 18:58 | 显示全部楼层
楼主是超人
发表于 2017-4-13 18:42 | 显示全部楼层
真心顶
发表于 2017-4-13 19:05 | 显示全部楼层
难得一见的好帖
发表于 2017-4-13 19:05 | 显示全部楼层
说的非常好
发表于 2017-5-10 16:30 | 显示全部楼层
真心顶
发表于 2017-5-10 16:16 | 显示全部楼层
难得一见的好帖
发表于 2017-5-10 16:25 | 显示全部楼层
很好哦
发表于 2017-5-10 16:40 | 显示全部楼层
不错不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-29 00:19 , Processed in 0.098303 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表