Search Unity

A Free Simple Smooth Mouselook + FreeLook View

Discussion in 'Scripting' started by khela, May 20, 2014.

  1. khela

    khela

    Joined:
    Oct 23, 2013
    Posts:
    8
    Hi, i've found this script on the forum:

    A Free Simple Smooth Mouselook

    And i would to implement a "FreeLook View" that allow me to rotate camera to left and right without changing player direction when i press a button. When i release the button rotation of camera should return to match the player rotation.

    How i can do that?
     
  2. khela

    khela

    Joined:
    Oct 23, 2013
    Posts:
    8
    Anyone please?

    Edit: i've create this for my own and i'm at good point the script works very well but i need a way to add smothing along X Axes

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class SmoothCameraMovements : MonoBehaviour
    4. {
    5.    
    6.     //Variables
    7.  
    8.     [Range(0.1f, 10f)]
    9.     public float sensitivity = 5f;
    10.  
    11.     [Range(0.01f, 1f)]
    12.     public float smoothing = 0.05f;
    13.  
    14.     [Range(0.01f, 1f)]
    15.     public float ReturnSmoothing = 0.05f;
    16.  
    17.     public Vector2 clamp = new Vector2(70, 70);
    18.  
    19.     public bool inFreeLookMode = false;
    20.     public bool invertY = false;
    21.  
    22.     public KeyCode FreeLookKey = KeyCode.F;
    23.    
    24.     public Vector2 RotationVelocity;
    25.  
    26.     private Vector2 _mouse;
    27.     private Vector2 _smooth;   
    28.  
    29.     // Use this for initialization
    30.     void Start ()
    31.     {
    32.     }
    33.    
    34.    
    35.     // Update is called once per frame
    36.     void Update ()
    37.     {
    38.         if ( Input.GetMouseButtonUp( 0 ) )
    39.             Screen.lockCursor = true;
    40.  
    41.         if( Input.GetKeyDown( KeyCode.Escape ) )
    42.             Screen.lockCursor = false;
    43.  
    44.         // Getting Mouse axis
    45.         _mouse.x += Input.GetAxis("Mouse X") * sensitivity;
    46.         _mouse.y += (Input.GetAxis("Mouse Y") * sensitivity) * (invertY ? 1 : -1);
    47.  
    48.         // Clamp along Y axis
    49.         _mouse.y = Mathf.Clamp (_mouse.y, -clamp.y, clamp.y);
    50.  
    51.         if (Input.GetKeyDown(FreeLookKey))
    52.         {
    53.             if (rigidbody)
    54.                 rigidbody.freezeRotation = inFreeLookMode = true;
    55.  
    56.             // Here we don't increment targetRotation.x because we want to reset rotation when we aren't in Free Look Mode
    57.             _mouse.x = Input.GetAxis ("Mouse X") * sensitivity;
    58.  
    59.             // Smoothing
    60.             _smooth.x = Mathf.SmoothDamp( _smooth.x, _mouse.x, ref RotationVelocity.x, smoothing );
    61.             _smooth.y = Mathf.SmoothDamp( _smooth.y, _mouse.y, ref RotationVelocity.y, smoothing );
    62.         }
    63.         else if (Input.GetKey(FreeLookKey))
    64.         {
    65.             // Clamp along X axis because we are in Free Look Mode
    66.             _mouse.x = Mathf.Clamp( _mouse.x, -clamp.x, clamp.x );
    67.  
    68.             // Smoothing
    69.             _smooth.x = Mathf.SmoothDamp(_smooth.x, _mouse.x, ref RotationVelocity.x, smoothing );
    70.             _smooth.y = Mathf.SmoothDamp(_smooth.y, _mouse.y, ref RotationVelocity.y, smoothing );
    71.  
    72.             // Here we are in Free Look Mode so we can rotate camere around both axis
    73.             transform.localRotation = Quaternion.Euler ( _smooth.y, _smooth.x, 0 );
    74.  
    75.             // But we must lock player rotation
    76.             transform.parent.transform.Rotate(0, 0, 0);
    77.         }
    78.         else if (Input.GetKeyUp (FreeLookKey))
    79.         {
    80.             if (rigidbody)
    81.                 rigidbody.freezeRotation = inFreeLookMode = false;
    82.  
    83.             // Reset target to zero to match player view
    84.             _mouse = new Vector2(0f,0f);
    85.         }
    86.         else
    87.         {
    88.  
    89.             // Smoothing
    90.             _smooth.x = Mathf.SmoothDamp (_smooth.x, 0, ref RotationVelocity.x, smoothing);
    91.             _smooth.y = Mathf.SmoothDamp (_smooth.y, _mouse.y, ref RotationVelocity.y, smoothing);
    92.             Debug.Log(Mathf.Approximately(_smooth.x, 0));
    93.             // Rotate the camera from old angles to the current player direction for X Axis and to 0 degrees along Y Axis relative to parent
    94.             transform.localRotation = Quaternion.Euler (_smooth.y, _smooth.x, 0);
    95.  
    96.  
    97.             // Here i need to add smoothing
    98.             transform.parent.transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivity , 0);
    99.         }
    100.     }
    101. }
    Here i need to add smoothing and the problem is that i can't use _smooth.x anymore because i must find a method to wait smoothdamp for completing

    Code (csharp):
    1. transform.parent.transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivity , 0);
     
    Last edited: May 20, 2014
  3. khela

    khela

    Joined:
    Oct 23, 2013
    Posts:
    8
    Bump?
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    So, I am not quite getting what you want.

    I went in and tested it, and I think you are missing some logic. You need to rotate the parent only when you are not in free look mode. As well, none of this code needs to run if the cursor is locked. (furthermore, if the cursor is not locked, it also needs to be visible. and vice versa)


    I rewrote it to get rid of alot of the conditional junk and re-posting of code. I kept the smoothing and attached new smoothing for the parent. And stopped it's damping and smoothing effects when you go into cursor mode or free look mode. You could cancel the free look mode when you go into cursor mode. That would be the only change I would make.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class SmoothCameraMovements : MonoBehaviour{
    5.     //Variables
    6.    
    7.     [Range(0.1f, 10f)]
    8.     public float sensitivity = 5f;
    9.    
    10.     [Range(0.01f, 1f)]
    11.     public float smoothing = 0.05f;
    12.  
    13.     public Vector2 xclamp = new Vector2(-180, 180);
    14.     public Vector2 yclamp = new Vector2(-70, 70);
    15.    
    16.     public bool inFreeLookMode = false;
    17.     public bool invertY = false;
    18.    
    19.     public KeyCode FreeLookKey = KeyCode.F;
    20.    
    21.     public Vector2 RotationVelocity;
    22.    
    23.     private Vector2 _mouse;
    24.     private Vector2 _smooth;
    25.  
    26.     private float _pSmooth;
    27.     private float pVelocity;
    28.    
    29.     // Use this for initialization we should be looking to start?
    30.     void Start (){
    31.         Screen.lockCursor = true;
    32.         Screen.showCursor = false;
    33.     }
    34.  
    35.     void Update(){
    36.         // if we are not holding the right mouse button down, unlock the cursor
    37.         if( Input.GetKeyDown( KeyCode.Escape ) ){
    38.             Screen.lockCursor = false;
    39.             Screen.showCursor = true;
    40.  
    41.             pVelocity = 0;
    42.             _pSmooth = 0;
    43.         }
    44.         if ( Input.GetMouseButtonUp( 0 ) ){
    45.             Screen.lockCursor = true;
    46.             Screen.showCursor = false;
    47.         }
    48.         if ( !Screen.lockCursor) return;
    49.  
    50.  
    51.         if(Input.GetKeyDown (FreeLookKey)) inFreeLookMode = !inFreeLookMode;
    52.  
    53.         // Getting Mouse axis
    54.         _mouse.y += (Input.GetAxis("Mouse Y") * sensitivity) * (invertY ? 1 : -1);
    55.  
    56.         if (inFreeLookMode) {
    57.             // free look means we don't rotate the parent, just the camera
    58.             _mouse.x += Input.GetAxis("Mouse X") * sensitivity;
    59.             pVelocity = 0;
    60.             _pSmooth = 0;
    61.         } else {
    62.             // no free look, dont use mouse.x;
    63.             _mouse.x = 0;
    64.             _pSmooth = Mathf.SmoothDamp( _pSmooth, Input.GetAxis("Mouse X") * sensitivity, ref pVelocity, smoothing);
    65.             transform.parent.transform.Rotate(0, _pSmooth , 0);
    66.         }
    67.        
    68.         // Clamp axis
    69.         _mouse.x = Mathf.Clamp (_mouse.x, xclamp.x, xclamp.y);
    70.         _mouse.y = Mathf.Clamp (_mouse.y, yclamp.x, yclamp.y);
    71.  
    72.         // smooth the camera
    73.         _smooth.x = Mathf.SmoothDamp( _smooth.x, _mouse.x, ref RotationVelocity.x, smoothing );
    74.         _smooth.y = Mathf.SmoothDamp( _smooth.y, _mouse.y, ref RotationVelocity.y, smoothing );
    75.  
    76.         // as much as I love eulers... I hate them.
    77.         transform.localRotation = Quaternion.identity;
    78.         transform.Rotate(_smooth.y, _smooth.x, 0);
    79.     }
    80. }
    81.  
     
  5. khela

    khela

    Joined:
    Oct 23, 2013
    Posts:
    8
    Nice work, works well. But this is in a "Toggle" way. This can be a drastic solution. If you see my script in game, u could see that when you release the button the free look view return to match the "Player view"

    Edit: Sorry, your code do the same :)

    i've just changed this line

    Code (csharp):
    1. if(Input.GetKeyDown (FreeLookKey)) inFreeLookMode = !inFreeLookMode;
    with:

    Code (csharp):
    1. if(Input.GetKey(FreeLookKey))
    2.     inFreeLookMode = true;
    3.  
    4. if(Input.GetKeyUp(FreeLookKey))
    5.     inFreeLookMode = false;
    and now works as well. Thank you, i'm reading your code and i attempt to understand what i have missed
     
    Last edited: May 20, 2014