CameraMove.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. using UnityEngine;
  2. using System.Collections;
  3. public class CameraMove : MonoBehaviour
  4. {
  5. public float sensitivityMouse = 2f;
  6. public float sensitivetyKeyBoard = 0.1f;
  7. public float sensitivetyMouseWheel = 10f;
  8. void Update()
  9. {
  10. //滚轮实现镜头缩进和拉远
  11. if (Input.GetAxis("Mouse ScrollWheel") != 0)
  12. {
  13. GetComponent<Camera>().fieldOfView = GetComponent<Camera>().fieldOfView - (Input.GetAxis("Mouse ScrollWheel") * sensitivetyMouseWheel);
  14. }
  15. //按着鼠标右键实现视角转动
  16. if (Input.GetMouseButton(1))
  17. {
  18. transform.Rotate(-Input.GetAxis("Mouse Y") * sensitivityMouse, Input.GetAxis("Mouse X") * sensitivityMouse, 0);
  19. }
  20. //键盘按钮←/a和→/d实现视角水平移动,键盘按钮↑/w和↓/s实现视角水平旋转
  21. if (Input.GetAxis("Horizontal") != 0)
  22. {
  23. transform.Translate(Input.GetAxis("Horizontal") * sensitivetyKeyBoard * 0.5f, 0, 0);
  24. }
  25. if (Input.GetAxis("Vertical") != 0)
  26. {
  27. transform.Translate(0, 0, Input.GetAxis("Vertical") * sensitivetyKeyBoard * 0.5f);
  28. }
  29. }
  30. }