Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.0 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. /// MouseLook rotates the transform based on the mouse delta.
  4. /// Minimum and Maximum values can be used to constrain the possible rotation
  5. /// To make an FPS style character:
  6. /// - Create a capsule.
  7. /// - Add the MouseLook script to the capsule.
  8. /// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
  9. /// - Add FPSInputController script to the capsule
  10. /// -> A CharacterMotor and a CharacterController component will be automatically added.
  11. /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
  12. /// - Add a MouseLook script to the camera.
  13. /// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
  14. [AddComponentMenu("Camera-Control/Mouse Look")]
  15. public class MouseLook : MonoBehaviour {
  16. public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
  17. public RotationAxes axes = RotationAxes.MouseXAndY;
  18. public float sensitivityX = 15F;
  19. public float sensitivityY = 15F;
  20. public float minimumX = -360F;
  21. public float maximumX = 360F;
  22. public float minimumY = -60F;
  23. public float maximumY = 60F;
  24. float rotationY = 0F;
  25. void Update ()
  26. {
  27. if (axes == RotationAxes.MouseXAndY)
  28. {
  29. float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
  30. rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  31. rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
  32. transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
  33. }
  34. else if (axes == RotationAxes.MouseX)
  35. {
  36. transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
  37. }
  38. else
  39. {
  40. rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  41. rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
  42. transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
  43. }
  44. }
  45. void Start ()
  46. {
  47. // Make the rigid body not change rotation
  48. if (GetComponent<Rigidbody>())
  49. GetComponent<Rigidbody>().freezeRotation = true;
  50. }
  51. }