Guest User

Untitled

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