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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Slowing down the "crouch" movement.

Discussion in 'Scripting' started by Fyurien, Aug 23, 2015.

  1. Fyurien

    Fyurien

    Joined:
    Aug 16, 2015
    Posts:
    20
    I'm just hoping to be pointed in the right direction.

    I have a FPS character controller and have "crouch" movement. Problem is it just snaps down and up. Its very very fast and feels very artificial. What I would like to do is slow down this movement but have no clue even where to start. Here is my code..

    Code (CSharp):
    1. using TouchControlsKit;
    2.  
    3. public class buttonCrouch : MonoBehaviour {
    4.  
    5.     void Update() {
    6.             CharacterController controller = GetComponent<CharacterController>();
    7.  
    8.                 if( InputManager.GetButton( "crouchButton" ) )
    9.  
    10.                     {
    11.                         controller.height = 1.0f;
    12.                     }
    13.                 else
    14.                     {
    15.                         controller.height = 2.0f;
    16.                     }
    17.                  
    18.                 }
    19.  
    20.     }
    It would be nice to have the player crouch in a slower manner. That's really the goal.

    Thank you in advance for any suggestions.
     
    Last edited: Aug 23, 2015
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Well, I haven't used that CharacterController, but it appears to have a "height" property that controls how much he's crouching.

    So, the issue then becomes: how do you interpolate this smoothly over multiple frames? I would do this by using Mathf.MoveTowards. Something like:

    Code (CSharp):
    1. void Update() {
    2.     CharacterController controller = GetComponent<CharacterController>();
    3.     if( InputManager.GetButton( "crouchButton" ) )
    4.     {
    5.         controller.height = Mathf.MoveTowards(controller.height, 1.0f, Time.deltaTime / crouchTime);
    6.     }
    7.     else
    8.     {
    9.         controller.height = Mathf.MoveTowards(controller.height, 2.0f, Time.deltaTime / crouchTime);
    10.     }
    11. }
    where crouchTime is a property that controls how long it should take your character to fully stand up or crouch down (start with a value like 0.5f, and then tweak as desired).
     
    Fyurien likes this.
  3. Fyurien

    Fyurien

    Joined:
    Aug 16, 2015
    Posts:
    20
    Fantastic solution!

    Its extremely smooth at 0.5f like you suggested. Here is the bit though, coming backup, it jitters. Like it stepping up to the right height. Not sure if that explains it well.

    So going down is smooth like a ramp. And coming backup it like a stair case. So what I did is this...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using TouchControlsKit;
    4.  
    5. public class buttonCrouch : MonoBehaviour {
    6.     public AudioSource[] sounds;
    7.     public AudioSource soundCrouchDown1;
    8.     public AudioSource soundCrouchUp2;
    9.     public AudioSource soundJump3;
    10.     public float crouchTimeDown = 0.5F;
    11.     public float crouchTimeUp = 0.1F;
    12.  
    13.     void Start()
    14.     {
    15.         sounds = GetComponents<AudioSource>();
    16.         soundCrouchDown1 = sounds[0];
    17.         soundCrouchUp2 = sounds[1];
    18.         soundJump3 = sounds[3];
    19.     }
    20.  
    21.     void Update() {
    22.             CharacterController controller = GetComponent<CharacterController>();
    23.  
    24.                 if( InputManager.GetButton( "crouchButton" ) )
    25.                     {
    26.                         controller.height = Mathf.MoveTowards(controller.height, 1.0f, Time.deltaTime / crouchTimeDown);
    27.                         soundCrouchDown1.Play();
    28.                     }
    29.                 else
    30.                     {
    31.                         controller.height = Mathf.MoveTowards(controller.height, 2.0f, Time.deltaTime / crouchTimeUp);
    32.                         soundCrouchUp2.Play();
    33.                     }
    34.                  
    35.             }
    36.  
    37.     }
    So now coming backup is pretty smooth, and quick, which actually doesn't look bad at all. Also added some sound effects. :)

    Thank you again for your help! :)
     
    JoeStrout likes this.