Advertisement
Guest User

camera script

a guest
Jul 12th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class MouseAimCamera : MonoBehaviour
  6. {
  7. public Transform target;
  8. public float distance = 2.0f;
  9. public float xSpeed = 20.0f;
  10. public float ySpeed = 20.0f;
  11. public float yMinLimit = -90f;
  12. public float yMaxLimit = 90f;
  13. public float distanceMin = 10f;
  14. public float distanceMax = 10f;
  15. public float smoothTime = 2f;
  16. float rotationYAxis = 0.0f;
  17. float rotationXAxis = 0.0f;
  18. float velocityX = 0.0f;
  19. float velocityY = 0.0f;
  20. public float StartPositionXAxis = 0f;
  21. public float StartPositionYAxis = 0f;
  22. void Start()
  23. {
  24. Vector3 angles = transform.eulerAngles;
  25. rotationYAxis = angles.y;
  26. rotationXAxis = angles.x;
  27. if (GetComponent<Rigidbody>())
  28. {
  29. GetComponent<Rigidbody>().freezeRotation = true;
  30. }
  31. }
  32. void LateUpdate()
  33. {
  34. if (target)
  35. {
  36. if (target)
  37. {
  38. velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
  39. velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
  40. }
  41. rotationYAxis += velocityX;
  42. rotationXAxis -= velocityY;
  43. rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
  44. Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
  45. Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
  46. Quaternion rotation = toRotation;
  47.  
  48. distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
  49. RaycastHit hit;
  50. if (Physics.Linecast(target.position, transform.position, out hit))
  51. {
  52. distance -= hit.distance;
  53. }
  54. Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
  55. Vector3 position = rotation * negDistance + target.position;
  56.  
  57. transform.rotation = rotation;
  58. transform.position = position;
  59. velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
  60. velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
  61. }
  62. }
  63. public static float ClampAngle(float angle, float min, float max)
  64. {
  65. if (angle < -360F)
  66. angle += 360F;
  67. if (angle > 360F)
  68. angle -= 360F;
  69. return Mathf.Clamp(angle, min, max);
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement