Unity3d使用鼠标滚轮移动相机

时间:2016-11-27 15:09:34

标签: unity3d camera

我正在尝试简化产品可视化。我想缩放或移动相机朝向物体。我的代码如下所示:

    public class mouseMover : MonoBehaviour {

        public Transform target;
        public float speed;


        void Update () {

            if (Input.GetAxis ("Mouse ScrollWheel") < 0) {
                float scroll = Input.GetAxis ("Mouse ScrollWheel");

                transform.LookAt (target);
                transform.Translate(0, 0, scroll * speed, Space.World);


            }

            if (Input.GetAxis ("Mouse ScrollWheel") > 0) {

                float scroll = Input.GetAxis ("Mouse ScrollWheel");
                transform.LookAt (target);
                transform.Translate(0, 0, scroll * speed, Space.World);
            }

    }

}

但是当我尝试缩放时,它只是“飞”&#34;在物体周围,当相机进入物体的另一侧时,它开始后退。

1 个答案:

答案 0 :(得分:1)

这是因为你使用Space.World而不是Space.Self:

void Update ()
{
    float scroll = Input.GetAxis ("Mouse ScrollWheel");
    transform.LookAt (target);
    transform.Translate(0, 0, scroll * speed, Space.Self);
}