csMouseOrbit.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEngine;
  2. using System.Collections;
  3. public class csMouseOrbit : MonoBehaviour
  4. {
  5. public Transform Target;
  6. public float distance;
  7. public float xSpeed = 250.0f;
  8. public float ySpeed = 120.0f;
  9. public float yMinLimit = -20.0f;
  10. public float yMaxLimit = 80.0f;
  11. private float x = 0.0f;
  12. private float y = 0.0f;
  13. public float CameraDist = 10;
  14. // Use this for initialization
  15. void Start()
  16. {
  17. Vector3 angles = transform.eulerAngles;
  18. x = angles.x;
  19. y = angles.y;
  20. distance = 30;
  21. if (this.GetComponent<Rigidbody>() == true)
  22. GetComponent<Rigidbody>().freezeRotation = true;
  23. }
  24. // Update is called once per frame
  25. void LateUpdate()
  26. {
  27. if (Target)
  28. {
  29. x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
  30. y += Input.GetAxis("Mouse Y") * ySpeed * 0.05f;
  31. y = ClampAngle(y, yMinLimit, yMaxLimit);
  32. Quaternion rotation = Quaternion.Euler(y, x, 0);
  33. Vector3 position = rotation * new Vector3(0, 0, -distance) + Target.position;
  34. transform.rotation = rotation;
  35. transform.position = position;
  36. distance = CameraDist;
  37. }
  38. }
  39. float ClampAngle(float ag, float min, float max)
  40. {
  41. if (ag < -360)
  42. ag += 360;
  43. if (ag > 360)
  44. ag -= 360;
  45. return Mathf.Clamp(ag, min, max);
  46. }
  47. }