Search Unity

(HELP) I Want To Change Player Height Over Time But Don't Know How

Discussion in 'Scripting' started by LemonMontage420, Jun 7, 2020.

  1. LemonMontage420

    LemonMontage420

    Joined:
    May 28, 2020
    Posts:
    56
    Here's My Code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Player_Crouch : MonoBehaviour
    4. {
    5.     private CharacterController m_CharacterController;
    6.     public KeyCode crouchKey = KeyCode.C;
    7.     public KeyCode jumpKey = KeyCode.Space;
    8.     private bool m_Crouch = false;
    9.     private float m_OriginalHeight;
    10.     [SerializeField] private float m_CrouchHeight = 0.9f;
    11.  
    12.     void Start()
    13.     {
    14.  
    15.         m_CharacterController = GetComponent<CharacterController>();
    16.         m_OriginalHeight = m_CharacterController.height;
    17.            
    18.            
    19.     }
    20.  
    21.  
    22.     void Update()
    23.     {
    24.         if (Input.GetKeyDown(crouchKey))
    25.         {
    26.             m_Crouch = !m_Crouch;
    27.             CheckCrouch();
    28.         }
    29.  
    30.     }
    31.    
    32.     void CheckCrouch()
    33.     {
    34.         if (m_Crouch == true)
    35.         {
    36.             m_CharacterController.height = m_CrouchHeight;
    37.         }
    38.         else
    39.         {
    40.             m_CharacterController.height = m_OriginalHeight;
    41.         }
    42.        
    43.     }
    44. }
    So basically, I want to change the height of my character over time instead of "snapping" to the set height. I know you need to use a "Lerp" function, but I don't know how to do it (excuse my incompetence if the answer is simple, this is my 6th day learning how to program)
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    Have a look at this. It's tricky in the beginning:

    https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html

    Code (CSharp):
    1. Mathf.Lerp(m_OriginalHeight, m_CrouchHeight, t)
    t is a range between 0 and 1 - so 0 would equal the value m_OriginalHeight, and 1 would equal the value of m_CrouchHeight;. Lerp interpolates the value between your two variables, so 0.5 would be m_OriginalHeight + m_CrouchHeight / 2.

    If you experiment with 't' and mess with it in the inspector, you can see the height change in game. You can control it easily and gradually multiplying 't' by Time.time.
     
  3. LemonMontage420

    LemonMontage420

    Joined:
    May 28, 2020
    Posts:
    56
    I'm sorry to bother you, but I have no idea how to rewrite my code like that, (I've been learning for less than a week now), would it be possible for you to write me this script?
     
  4. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    Code (CSharp):
    1.     using UnityEngine;
    2.  
    3.     public class Player_Crouch : MonoBehaviour
    4.     {
    5.         private CharacterController m_CharacterController;
    6.         public KeyCode crouchKey = KeyCode.C;
    7.         public KeyCode jumpKey = KeyCode.Space;
    8.         private bool m_Crouch = false;
    9.         private float m_OriginalHeight;
    10.         [SerializeField] private float m_CrouchHeight = 0.9f;
    11.  
    12.         public float transLerp, raiseHeight, lowerHeight, speed;
    13.  
    14.         void Start ()
    15.         {
    16.  
    17.             m_CharacterController = GetComponent<CharacterController> ();
    18.             m_OriginalHeight = m_CharacterController.height;
    19.  
    20.         }
    21.  
    22.         void Update ()
    23.         {
    24.             if (Input.GetKeyDown (crouchKey))
    25.             {
    26.                 m_Crouch = !m_Crouch;
    27.                 CheckCrouch ();
    28.             }
    29.  
    30.         }
    31.  
    32.         void CheckCrouch ()
    33.         {
    34.             if (m_Crouch == true)
    35.             {
    36.                 raiseHeight += Time.deltaTime;
    37.                 transLerp = Mathf.Lerp (m_CrouchHeight, m_OriginalHeight, raiseHeight / speed);
    38.                 m_CharacterController.height = transLerp;
    39.                 lowerHeight = 0;
    40.             }
    41.             else
    42.             {
    43.                 lowerHeight += Time.deltaTime;
    44.                 transLerp = Mathf.Lerp (m_OriginalHeight, m_CrouchHeight, lowerHeight/ speed);
    45.                 m_CharacterController.height = transLerp;
    46.                 raiseHeight = 0;
    47.             }
    48.  
    49.         }
    50.     }
    You could put the lerp routine in it's seperate function, but this should work.
    raiseHeight and lowerHeight are always 0, until the each relevant condition is true, then they equal Time.deltaTime. 'speed' slows it down by dividing the result.
    The quirky thing is using time as the modifier and how it works.
     
    Last edited: Jun 7, 2020
  5. LemonMontage420

    LemonMontage420

    Joined:
    May 28, 2020
    Posts:
    56
    Thank You So Much