ChameL1oN

Mouse_Orbit

Feb 14th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. NOT MY CODE
  2.  
  3.  
  4.  
  5. using UnityEngine;
  6. using System.Collections;
  7.  
  8. public class MouseOrbitCSHARP : MonoBehaviour
  9. {
  10. public Transform Target;
  11. public float Distance = 5.0f;
  12. public float xSpeed = 250.0f;
  13. public float ySpeed = 120.0f;
  14. public float yMinLimit = -20.0f;
  15. public float yMaxLimit = 80.0f;
  16.  
  17. private float x;
  18. private float y;
  19.  
  20. void Awake()
  21. {
  22. Vector3 angles = transform.eulerAngles;
  23. x = angles.x;
  24. y = angles.y;
  25.  
  26. if(GetComponent<Rigidbody>() != null)
  27. {
  28. rigidbody.freezeRotation = true;
  29. }
  30. }
  31.  
  32. void LateUpdate()
  33. {
  34. if(Target != null)
  35. {
  36. x += (float)(Input.GetAxis("Mouse X") * xSpeed * 0.02f);
  37. y -= (float)(Input.GetAxis("Mouse Y") * ySpeed * 0.02f);
  38.  
  39. y = ClampAngle(y, yMinLimit, yMaxLimit);
  40.  
  41. Quaternion rotation = Quaternion.Euler(y, x, 0);
  42. Vector3 position = rotation * (new Vector3(0.0f, 0.0f, -Distance)) + Target.position;
  43.  
  44. transform.rotation = rotation;
  45. transform.position = position;
  46. }
  47. }
  48.  
  49. private float ClampAngle(float angle, float min, float max)
  50. {
  51. if(angle < -360)
  52. {
  53. angle += 360;
  54. }
  55. if(angle > 360)
  56. {
  57. angle -= 360;
  58. }
  59. return Mathf.Clamp (angle, min, max);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment