Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Smooth Mouse Look modified for the Wiki

Discussion in 'Scripting' started by Fraconte, Aug 13, 2014.

  1. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    I was trying to create an account to put back this modified version on the Wiki but with no result. So i post it here.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [AddComponentMenu("Camera-Control/Smooth Mouse Look")]
    6. public class SmoothMouseLook : MonoBehaviour
    7. {
    8.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    9.     public RotationAxes axes = RotationAxes.MouseXAndY;
    10.     public float sensitivityX = 15F;
    11.     public float sensitivityY = 15F;
    12.  
    13.     public bool freeX = true;
    14.     public bool freeY = false;
    15.  
    16.     public float minimumX = -180F;
    17.     public float maximumX = 180F;
    18.    
    19.     public float minimumY = -60F;
    20.     public float maximumY = 60F;
    21.    
    22.     float rotationX = 0F;
    23.     float rotationY = 0F;
    24.    
    25.     List<float> rotArrayX = new List<float>();
    26.     float rotAverageX = 0F;  
    27.    
    28.     List<float> rotArrayY = new List<float>();
    29.     float rotAverageY = 0F;
    30.    
    31.     public float frameCounter = 20;
    32.    
    33.     Quaternion originalRotation;
    34.    
    35.     void Update ()
    36.     {
    37.         if (axes == RotationAxes.MouseXAndY)
    38.         {          
    39.             rotAverageY = 0f;
    40.             rotAverageX = 0f;
    41.            
    42.             rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
    43.             rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
    44.  
    45.             if (!freeY)
    46.                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    47.             if (!freeX)
    48.                 rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
    49.        
    50.             rotArrayY.Add(rotationY);
    51.             rotArrayX.Add(rotationX);
    52.            
    53.             if (rotArrayY.Count >= frameCounter)
    54.                 rotArrayY.RemoveAt(0);
    55.             if (rotArrayX.Count >= frameCounter)
    56.                 rotArrayX.RemoveAt(0);
    57.  
    58.             foreach (float rotY in rotArrayY)
    59.                 rotAverageY += rotY;
    60.             foreach (float rotX in rotArrayX)
    61.                 rotAverageX += rotX;
    62.  
    63.             rotAverageY /= rotArrayY.Count;
    64.             rotAverageX /= rotArrayX.Count;
    65.            
    66.             Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
    67.             Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
    68.            
    69.             transform.localRotation = originalRotation * xQuaternion * yQuaternion;
    70.         }
    71.         else if (axes == RotationAxes.MouseX)
    72.         {          
    73.             rotAverageX = 0f;
    74.            
    75.             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    76.  
    77.             if (!freeX)
    78.                 rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
    79.  
    80.             rotArrayX.Add(rotationX);
    81.            
    82.             if (rotArrayX.Count >= frameCounter)
    83.                 rotArrayX.RemoveAt(0);
    84.  
    85.             foreach (float rotX in rotArrayX)
    86.                 rotAverageX += rotX;
    87.  
    88.             rotAverageX /= rotArrayX.Count;
    89.            
    90.             Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
    91.             transform.localRotation = originalRotation * xQuaternion;          
    92.         }
    93.         else
    94.         {          
    95.             rotAverageY = 0f;
    96.            
    97.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    98.  
    99.             if (!freeY)
    100.                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    101.  
    102.             rotArrayY.Add(rotationY);
    103.            
    104.             if (rotArrayY.Count >= frameCounter)
    105.                 rotArrayY.RemoveAt(0);
    106.  
    107.             foreach (float rotY in rotArrayY)
    108.                 rotAverageY += rotY;
    109.  
    110.             rotAverageY /= rotArrayY.Count;
    111.            
    112.             Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
    113.             transform.localRotation = originalRotation * yQuaternion;
    114.         }
    115.     }
    116.    
    117.     void Start ()
    118.     {          
    119.         if (rigidbody)
    120.             rigidbody.freezeRotation = true;
    121.         originalRotation = transform.localRotation;
    122.     }
    123. }