先看效果
实现方式
1.首先创建一个脚本
2.编辑脚本内容
付上脚本代码
private float RotationX = 0;public float speed = 2f;//移动速度// Use this for initializationvoid Start(){Cursor.lockState = CursorLockMode.Locked;//锁定鼠标到中心点Cursor.visible = false;//隐藏鼠标}// Update is called once per framevoid Update(){RotationX -= Input.GetAxis("Mouse Y");RotationX = Mathf.Clamp(RotationX, -45f, 45f);float delta = Input.GetAxis("Mouse X");float RotationY = transform.localEulerAngles.y + delta;transform.localEulerAngles = new Vector3(RotationX, RotationY, 0);if (Input.GetKey(KeyCode.W)){transform.Translate(Vector3.forward * Time.deltaTime * speed);}if (Input.GetKey(KeyCode.S)){transform.Translate(Vector3.back * Time.deltaTime * speed);}if (Input.GetKey(KeyCode.A)){transform.Translate(Vector3.left * Time.deltaTime * speed);}if (Input.GetKey(KeyCode.D)){transform.Translate(Vector3.right * Time.deltaTime * speed);}if (Input.GetKey(KeyCode.E)){transform.Translate(Vector3.up * Time.deltaTime * speed);}if (Input.GetKey(KeyCode.Q)){transform.Translate(Vector3.down * Time.deltaTime * speed);}}
3.挂载脚本(一定挂在你要控制的相机上)
4.运行起来就可以看到效果啦