unity的摄像机脚本

骑猪看日落 2023-08-17 15:42 253阅读 0赞
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.EventSystems;
  4. public class MainCamera : MonoBehaviour
  5. {
  6. public Transform target;
  7. public float targetHeight = 1.2f;
  8. public float distance = 8.0f;
  9. public float offsetFromWall = 0.1f;
  10. public float maxDistance = 15;
  11. public float minDistance = 3f;
  12. public float xSpeed = 200.0f;
  13. public float ySpeed = 200.0f;
  14. public int yMinLimit = 0;
  15. public int yMaxLimit = 80;
  16. public int zoomRate = 40;
  17. public float rotationDampening = 3.0f;
  18. public float zoomDampening = 5.0f;
  19. public LayerMask collisionLayers = -1;
  20. private float xDeg = -53.2f;
  21. private float yDeg = 22.4f;
  22. private float currentDistance;
  23. private float desiredDistance;
  24. private float correctedDistance;
  25. void Start()
  26. {
  27. currentDistance = distance; //当前距离
  28. desiredDistance = distance; //默认距离
  29. correctedDistance = distance; //需要修正的距离
  30. }
  31. void LateUpdate()
  32. {
  33. target = GameObject.FindGameObjectWithTag("Player").transform;
  34. Vector3 vTargetOffset;
  35. if (!target)
  36. {
  37. return;
  38. }
  39. if (Input.GetMouseButton(1))
  40. {
  41. xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
  42. yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  43. }
  44. yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
  45. xDeg = ClampAngle(xDeg, -360, 360);
  46. Quaternion rotation = Quaternion.Euler(yDeg, xDeg, 0);
  47. desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance); //滚珠移动摄像头与人的距离
  48. correctedDistance = desiredDistance;
  49. vTargetOffset = new Vector3(0, -targetHeight, 0);
  50. Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset); //摄像机与人之间的向量
  51. RaycastHit collisionHit;
  52. Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z); //修改人身上的射线发出点坐标
  53. bool isCorrected = false;
  54. if (Physics.Linecast(trueTargetPosition, position, out collisionHit, collisionLayers.value))
  55. {
  56. correctedDistance = Vector3.Distance(trueTargetPosition, collisionHit.point) - offsetFromWall;
  57. isCorrected = true;
  58. }
  59. currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
  60. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  61. position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);
  62. transform.rotation = rotation;
  63. transform.position = position;
  64. }
  65. private static float ClampAngle(float angle, float min, float max)
  66. {
  67. if (angle < -360)
  68. angle += 360;
  69. if (angle > 360)
  70. angle -= 360;
  71. return Mathf.Clamp(angle, min, max);
  72. }
  73. }

  

转载于:https://www.cnblogs.com/allyh/p/11487794.html

发表评论

表情:
评论列表 (有 0 条评论,253人围观)

还没有评论,来说两句吧...

相关阅读

    相关 unity3d摄像机控制

    其实不需要什么空物体了,war3式的平移效果其实是高度不变平行地面的一个平面上四处移动的问题了,光translate摄像机是不行的 贴上我完整的代码吧 复