Advertisement
rhose87

MouseLookiPhone

Feb 2nd, 2012
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 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 a rigid body to the capsule
  10. /// - Add the MouseLook script to the capsule.
  11. ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
  12. /// - Add FPSWalker script to the capsule
  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 MouseLookiPhone : 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 rotationX = 0F;
  32.     float rotationY = 0F;
  33.    
  34.     private float inputX = 0F;
  35.     private float inputY = 0F;
  36.    
  37.     Quaternion originalRotation;
  38.  
  39.     public void CustomUpdate ()
  40.     {
  41.         if (axes == RotationAxes.MouseXAndY)
  42.         {
  43.             // Read the mouse input axis
  44.             rotationX += inputX * sensitivityX;
  45.             rotationY += inputY * sensitivityY;
  46.  
  47.             rotationX = ClampAngle (rotationX, minimumX, maximumX);
  48.             rotationY = ClampAngle (rotationY, minimumY, maximumY);
  49.            
  50.             Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
  51.             Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
  52.            
  53.             transform.localRotation = originalRotation * xQuaternion * yQuaternion;
  54.         }
  55.         else if (axes == RotationAxes.MouseX)
  56.         {
  57.             rotationX += inputX * sensitivityX;
  58.             rotationX = ClampAngle (rotationX, minimumX, maximumX);
  59.  
  60.             Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
  61.             transform.localRotation = originalRotation * xQuaternion;
  62.         }
  63.         else
  64.         {
  65.             rotationY += inputY * sensitivityY;
  66.             rotationY = ClampAngle (rotationY, minimumY, maximumY);
  67.  
  68.             Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
  69.             transform.localRotation = originalRotation * yQuaternion;
  70.         }
  71.     }
  72.    
  73.     void Start ()
  74.     {
  75.         // Make the rigid body not change rotation
  76.         originalRotation = transform.localRotation;
  77.     }
  78.    
  79.     public static float ClampAngle (float angle, float min, float max)
  80.     {
  81.         if (angle < -360F)
  82.             angle += 360F;
  83.         if (angle > 360F)
  84.             angle -= 360F;
  85.         return Mathf.Clamp (angle, min, max);
  86.     }
  87.    
  88.     public void SetInput (Vector2 rightStickInput)
  89.     {
  90.         inputX = rightStickInput.x * 0.1F;
  91.         inputY = rightStickInput.y * 0.1F;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement