美文网首页
unity 自由移动相机

unity 自由移动相机

作者: WOTTOW | 来源:发表于2020-03-26 22:09 被阅读0次
public float speedNormal = 10.0f;
   public float speedFast = 50.0f;

   public float mouseSensitivityX = 5.0f;
   public float mouseSensitivityY = 5.0f;

   private float rotY = 0.0f;
   private float rotX = 0.0f;
   private float v;
   private float h;
   private float speed;

   private Vector3 trans;

   void Start()
   {
       rotX = transform.localEulerAngles.y;
       rotY = transform.localEulerAngles.x;
   }

   void Update()
   {
       if (Input.GetMouseButton(0))
       {
           rotX += Input.GetAxis("Mouse X") * mouseSensitivityX;
           rotY -= Input.GetAxis("Mouse Y") * mouseSensitivityY;
           transform.rotation = Quaternion.Euler(rotY, rotX, 0);
       }

       v = Input.GetAxis("Vertical");
       h = Input.GetAxis("Horizontal");

       if (v != 0.0f)
       {
           speed = Input.GetKey(KeyCode.LeftShift) ? speedFast : speedNormal;
           trans = new Vector3(0.0f, 0.0f, v * speed * Time.deltaTime);
           transform.localPosition += transform.localRotation * trans;
       }
       // strafe left/right
       if (h != 0.0f)
       {
           speed = Input.GetKey(KeyCode.LeftShift) ? speedFast : speedNormal;
           trans = new Vector3(h * speed * Time.deltaTime, 0.0f, 0.0f);
           transform.localPosition += transform.localRotation * trans;
       }
   }

相关文章

网友评论

      本文标题:unity 自由移动相机

      本文链接:https://www.haomeiwen.com/subject/ytftuhtx.html