当前位置:   article > 正文

Unity三种摄像机旋转方式_unity 相机旋转

unity 相机旋转

1.按下鼠标右键可以实现摄像机上下左右旋转

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CameraRotate : MonoBehaviour
  5. {
  6. //旋转速度
  7. public float rotationSpeed = 5f;
  8. //上下旋转角度限制
  9. public float maxVerticalAngle = 90f;
  10. public float minVerticalAngle = -90f;
  11. //旋转缓冲速度
  12. public float lerpSpeed = 10f;
  13. private float targetRotationX = 0f;
  14. private float targetRotationY = 0f;
  15. void Update()
  16. {
  17. if (Input.GetMouseButton(1))
  18. {
  19. // 获取鼠标输入的旋转增量
  20. float rotationXInput = -Input.GetAxis("Mouse Y");
  21. float rotationYInput = Input.GetAxis("Mouse X");
  22. // 根据旋转速度进行摄像机的旋转
  23. targetRotationX += rotationXInput * rotationSpeed;
  24. targetRotationY += rotationYInput * rotationSpeed;
  25. // 对上下旋转角度进行限制
  26. targetRotationX = Mathf.Clamp(targetRotationX, minVerticalAngle, maxVerticalAngle);
  27. // 根据旋转角度更新摄像机的欧拉角,Quaternion.Lerp可以使摄像机旋转更加平滑
  28. Quaternion targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0f);
  29. transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed * Time.deltaTime);
  30. }
  31. }
  32. }

2.按下鼠标右键可以实现摄像机围绕某个物体上下左右旋转

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CameraRotate1 : MonoBehaviour
  5. {
  6. public Transform target;
  7. public float rotationSpeed = 5f;
  8. public float maxVerticalAngle = 90f;
  9. public float minVerticalAngle = -90f;
  10. public float lerpSpeed = 200f;
  11. public float distance = 10;
  12. private float targetRotationX = 0f;
  13. private float targetRotationY = 0f;
  14. void Start()
  15. {
  16. if (target == null)
  17. Debug.LogError("Please assign a target to the orbit camera!");
  18. }
  19. void Update()
  20. {
  21. if (Input.GetMouseButton(1))
  22. {
  23. float rotationXInput = -Input.GetAxis("Mouse Y");
  24. float rotationYInput = Input.GetAxis("Mouse X");
  25. targetRotationX += rotationXInput * rotationSpeed;
  26. targetRotationY += rotationYInput * rotationSpeed;
  27. targetRotationX = Mathf.Clamp(targetRotationX, minVerticalAngle, maxVerticalAngle);
  28. Quaternion targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0f);
  29. transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed * Time.deltaTime);
  30. }
  31. transform.position = target.position - transform.forward * distance;
  32. }
  33. }

3.摄像头始终跟随在某个物体的正后方

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CameraRotate2 : MonoBehaviour
  5. {
  6. public Transform target;
  7. public float followSpeed = 2f;
  8. public float followHeight = 4f;
  9. public float distance = 8f;
  10. private Vector3 velocity = Vector3.zero;
  11. void Start()
  12. {
  13. if (target == null)
  14. Debug.LogError("Please assign a target to the orbit camera!");
  15. }
  16. void LateUpdate()
  17. {
  18. Vector3 targetPosition = target.position - (target.forward * distance)+new Vector3(0,followHeight,0);
  19. transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, 1f / followSpeed);
  20. transform.LookAt(target);
  21. }
  22. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/122924
推荐阅读
相关标签
  

闽ICP备14008679号