当前位置:   article > 正文

unity控制相机移动_unity 相机移动

unity 相机移动

1.鼠标控制相机的旋转

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Mouselook : MonoBehaviour
  5. {
  6. public enum RotationAxes
  7. {
  8. MouseXAndY = 0,
  9. MouseX = 1,
  10. MouseY = 2
  11. }
  12. public RotationAxes axes = RotationAxes.MouseXAndY;
  13. public float sensitivityHor = 1f;
  14. public float sensitivityVert = 1f;
  15. public float minmumVert = -45f;
  16. public float maxmumVert = 45f;
  17. private float _rotationX = 0;
  18. // Use this for initialization
  19. void Start()
  20. {
  21. Cursor.lockState = CursorLockMode.Locked;//锁定指针到视图中心
  22. Cursor.visible = false;//隐藏指针
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. if (axes == RotationAxes.MouseX)
  28. {
  29. transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);
  30. }
  31. else if (axes == RotationAxes.MouseY)
  32. {
  33. _rotationX = _rotationX - Input.GetAxis("Mouse Y") * sensitivityVert;
  34. _rotationX = Mathf.Clamp(_rotationX, minmumVert, maxmumVert);
  35. float rotationY = transform.localEulerAngles.y;
  36. transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
  37. }
  38. else
  39. {
  40. _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
  41. _rotationX = Mathf.Clamp(_rotationX, minmumVert, maxmumVert);
  42. float delta = Input.GetAxis("Mouse X") * sensitivityHor;
  43. float rotationY = transform.localEulerAngles.y + delta;
  44. transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
  45. }
  46. }
  47. }

2.键盘wasd控制相机移动

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class move_wasd : MonoBehaviour
  5. {
  6. public GameObject cube;
  7. void Update()
  8. {
  9. if (Input.GetKey(KeyCode.W))
  10. {
  11. cube.transform.Translate(Vector3.forward * Time.deltaTime);
  12. }
  13. if (Input.GetKey(KeyCode.S))
  14. {
  15. cube.transform.Translate(Vector3.back * Time.deltaTime);
  16. }
  17. if (Input.GetKey(KeyCode.A))
  18. {
  19. cube.transform.Translate(Vector3.left * Time.deltaTime);
  20. }
  21. if (Input.GetKey(KeyCode.D))
  22. {
  23. cube.transform.Translate(Vector3.right * Time.deltaTime);
  24. }
  25. }
  26. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/122919
推荐阅读
相关标签
  

闽ICP备14008679号