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

Question Smooth transition of two values. Currently glitchy behaviour.

Discussion in 'Scripting' started by Rakowu, Nov 29, 2020.

  1. Rakowu

    Rakowu

    Joined:
    Nov 15, 2017
    Posts:
    16
    Hello everybody,
    does anyone know how to smooth _FreeLook.m_XAxis.m_InputAxisValue from cinemachine?

    My goal is to use a blendtree which, depending on how the xAxis value of cinemachine is, is animated more or less strongly. But instead of a smooth transition it jumps because it triggers too precisely, I guess. So if you stop for a millisecond, the player stops for that millisecond as well, which makes the whole thing look glitchy and jumpy.

    I have tried Lerp, Smoothstep, SmoothDamp and with repeatinvoke. I have no more ideas.


    Currently my code:


    Code (CSharp):
    1. void MouseHorizontalMovement(){
    2.         if((_FreeLook.m_XAxis.m_InputAxisValue > 0.5 || _FreeLook.m_XAxis.m_InputAxisValue < -0.5)){
    3.                 animator.SetFloat("Horizontal", Mathf.Lerp(lastXAxis,Mathf.Clamp(_FreeLook.m_XAxis.m_InputAxisValue,-2,2),3f * Time.deltaTime));
    4.             }
    5.             if(_FreeLook.m_XAxis.m_InputAxisValue == 0){
    6.                 animator.SetFloat("Horizontal", Mathf.Lerp(lastXAxis,0,3f * Time.deltaTime));
    7.             }
    8.             lastXAxis = Mathf.Clamp(_FreeLook.m_XAxis.m_InputAxisValue,-2,2);
    9.     }
    hSpeed means when there is no input from the keyboard.

    Code (CSharp):
    1. void Update(){
    2.         if(hSpeed == 0){
    3.             if((_FreeLook.m_XAxis.m_InputAxisValue > 0.5 || _FreeLook.m_XAxis.m_InputAxisValue < -0.5)){
    4.                 animator.SetFloat("Horizontal", Mathf.Lerp(lastXAxis,Mathf.Clamp(_FreeLook.m_XAxis.m_InputAxisValue,-2,2),3f * Time.deltaTime));
    5.             }
    6.             if(_FreeLook.m_XAxis.m_InputAxisValue == 0){
    7.                 animator.SetFloat("Horizontal", Mathf.Lerp(lastXAxis,0,3f * Time.deltaTime));
    8.             }
    9.             lastXAxis = Mathf.Clamp(_FreeLook.m_XAxis.m_InputAxisValue,-2,2);
    10.         }
    11.     }
    If you want a video to show you what i mean, let me know.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
  3. Rakowu

    Rakowu

    Joined:
    Nov 15, 2017
    Posts:
    16
    Hey thanks for your reply. I will have a look :D
     
  4. Rakowu

    Rakowu

    Joined:
    Nov 15, 2017
    Posts:
    16
    Dude i love you.

    This link did it perfectly.
    https://forum.unity.com/threads/how...ative-horizontal-buttons.963033/#post-6271251

    Thank you so much, you saved me a lot of headache.



    Code (CSharp):
    1. float axis;
    2. const float Snappiness = 1f;
    3.  
    4.  
    5. int CameraDirection(){
    6.         if(_FreeLook.m_XAxis.m_InputAxisValue  < 0){
    7.             return -1;
    8.         }
    9.         if(_FreeLook.m_XAxis.m_InputAxisValue  > 0){
    10.             return 1;
    11.         }
    12.         return 0;
    13.     }
    14.  
    15. void FixedUpdate(){
    16.     float x = CameraDirection();
    17.     axis = Mathf.Lerp( axis, x, Snappiness * Time.deltaTime);
    18.     animator.SetFloat("Horizontal",axis);
    19. }
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    :) Awesome.
     
    Rakowu likes this.