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

Linear Interpolation in my Mouse Look Script

Discussion in 'Scripting' started by TritanTechnology, Mar 21, 2017.

  1. TritanTechnology

    TritanTechnology

    Joined:
    Feb 16, 2017
    Posts:
    19
    Hello, everyone! I have a mouse look script for my game that is working fine. A friend of mine told me that it will be more smooth if I add Linear Interpolation. I read about it on the Scriptin page, but I still need help. Here is my script -
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseOrbit : MonoBehaviour {
    5.    
    6.     [HideInInspector]
    7.     public GameObject target; //a target look at
    8.     public float xSpeed; //speed pan x
    9.     public float ySpeed; //speed pan y
    10.     public float yMinLimit; //y min limit
    11.     public float yMaxLimit; //y max limit
    12.    
    13.     public float scrollSpeed; //scroll speed
    14.     public float zoomMin;  //zoom min
    15.     public float zoomMax; //zoom max
    16.    
    17.     //Private variable
    18.     private float distance;
    19.     private float distanceLerp;
    20.     private Vector3 position;
    21.     private bool isActivated;
    22.     private float x;
    23.     private float y;
    24.     private bool setupCamera;
    25.  
    26.     // Use this for initialization
    27.     void Start () {
    28.        
    29.        
    30.         //Warning when not found target
    31.         if(target == null)
    32.         {
    33.             target = GameObject.FindGameObjectWithTag("Player");
    34.            
    35.             if(target == null)
    36.             {
    37.                 Debug.LogWarning("Don't found player tag please change player tag to Player");  
    38.             }
    39.         }
    40.        
    41.        
    42.         //Setup Pos
    43.          Vector3 angles = transform.eulerAngles;
    44.          x = angles.y;
    45.          y = angles.x;
    46.                
    47.         CalDistance();
    48.     }
    49.     void LateUpdate () {
    50.        
    51.         ScrollMouse();
    52.         RotateCamera();
    53.     }
    54.    
    55.     //Roate camera method
    56.     void RotateCamera()
    57.     {
    58.         if (Input.GetMouseButtonDown(1)){
    59.             isActivated = true;
    60.         }
    61.         // if mouse button is let UP then stop rotating camera
    62.         if (Input.GetMouseButtonUp(1))
    63.         {
    64.             isActivated = false;
    65.         }
    66.         if (target && isActivated) {
    67.            
    68.           y -= Input.GetAxis("Mouse Y") * ySpeed;
    69.  
    70.           x += Input.GetAxis("Mouse X") * xSpeed;
    71.  
    72.            
    73.    
    74.           y = ClampAngle(y, yMinLimit, yMaxLimit);
    75.    
    76.    
    77.            Quaternion rotation = Quaternion.Euler(y, x, 0);
    78.            
    79.             Vector3 calPos = new Vector3(0, 0, -distanceLerp);
    80.  
    81.              position = rotation * calPos + target.transform.position;
    82.    
    83.             transform.rotation = rotation;
    84.    
    85.             transform.position = position;
    86.    
    87.         } else
    88.         {
    89.             Quaternion rotation = Quaternion.Euler(y, x, 0);
    90.            
    91.             Vector3 calPos = new Vector3(0, 0, -distanceLerp);
    92.  
    93.              position = rotation * calPos + target.transform.position;
    94.    
    95.             transform.rotation = rotation;
    96.    
    97.             transform.position = position;
    98.         }
    99.     }
    100.    
    101.      //Calculate Distance Method
    102.      void CalDistance()
    103.     {
    104.         distance = zoomMax;
    105.         distanceLerp = distance;
    106.         Quaternion rotation = Quaternion.Euler(y, x, 0);  
    107.         Vector3 calPos = new Vector3(0, 0, -distanceLerp);
    108.         position = rotation * calPos + target.transform.position;
    109.         transform.rotation = rotation;
    110.         transform.position = position;
    111.     }
    112.    
    113.     //Scroll Mouse Method
    114.     void ScrollMouse()
    115.     {
    116.         distanceLerp = Mathf.Lerp(distanceLerp,distance,Time.deltaTime * 5);
    117.         if (Input.GetAxis("Mouse ScrollWheel") != 0 && !GUI_Menu.instance.CheckHoverItemShop() && !GUI_Menu.instance.CheckHoverSkillWindow())
    118.             {  
    119.                 // get the distance between camera and target
    120.                 distance = Vector3.Distance (transform.position , target.transform.position);  
    121.                 distance = ScrollLimit(distance - Input.GetAxis("Mouse ScrollWheel")*scrollSpeed, zoomMin, zoomMax);
    122.             }
    123.     }
    124.    
    125.      //Scroll Limit Method
    126.     float ScrollLimit(float dist, float min, float max)
    127.     {
    128.         if (dist < min)
    129.             dist= min;
    130.         if (dist > max)
    131.             dist= max;
    132.         return dist;
    133.     }
    134.    
    135.    
    136.     //Clamp Angle Method
    137.     float ClampAngle(float angle,float min,float max)
    138.     {
    139.         if(angle < -360)
    140.             angle += 360;
    141.         if(angle > 360)
    142.             angle -= 360;
    143.         return Mathf.Clamp(angle,min,max);
    144.     }
    145. }
    146.  
    I really hope that you are going to help me! Thank you! :))
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Some things for you to consider
    Your camera should be already smooth, because you're using GetAxis, instead of GetAxisRaw, which is a smoothed version
    A lot of gamers don't like or even hate when something is smoothed, think about it, when did you ever hear that "I love this mouse movement smoothing, it makes my life a little harder each day". A smooth movement is less controllable
     
  3. TritanTechnology

    TritanTechnology

    Joined:
    Feb 16, 2017
    Posts:
    19
    Thank you for the comment! But can you help me with implementing it in this script just to see how it works. :))