Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Changing a float so my character runs instead of walks

Discussion in '2D' started by MasterElement, Feb 20, 2021.

  1. MasterElement

    MasterElement

    Joined:
    Jun 13, 2016
    Posts:
    51
    So I have a 2D platformer, and I was following a tutorial where they show you how to make the character move and walk. However I want to add a key press that increase movement speed so the character runs. I have tried to use my knowledge that I process to change the float within the code that is already used. However it does nothing. Can someone please explain to me how to make the variable increase with the key press then decrease when it's released?

    Hear is my code. using.


    Code (CSharp):
    1. public class SHM_Script : MonoBehaviour
    2.     {
    3.         private Rigidbody2D SMRigidbody;
    4.    
    5.         private Animator SMAnimator;
    6.    
    7.         [SerializeField]
    8.         private float movementSpeed;
    9.    
    10.         private bool facingRight;
    11.    
    12.         // Start is called before the first frame update
    13.         void Start()
    14.         {
    15.             facingRight = true;
    16.             SMRigidbody = GetComponent<Rigidbody2D>();
    17.             SMAnimator = GetComponent<Animator>();
    18.             movementSpeed = 10;
    19.     }
    20.    
    21.         // Update is called once per frame
    22.         void Update()
    23.         {
    24.             HandleInput();
    25.         }
    26.         void FixedUpdate()
    27.         {
    28.             float horizontal = Input.GetAxis("Horizontal");
    29.    
    30.             HandleMovement(horizontal);
    31.    
    32.             Flip(horizontal);
    33.         }
    34.    
    35.         private void HandleInput()
    36.         {
    37.             if (Input.GetKeyDown(KeyCode.RightShift))
    38.             {
    39.                 movementSpeed = 20;
    40.             }
    41.             else
    42.             {
    43.                 movementSpeed = 10;
    44.             }
    45.         }
    46.             private void HandleMovement(float horizontal)
    47.         {
    48.             SMRigidbody.velocity = new Vector2(horizontal * movementSpeed, SMRigidbody.velocity.y);
    49.    
    50.             SMAnimator.SetFloat("speed", Mathf.Abs(horizontal));
    51.         }
    52.    
    53.         private void Flip(float horizontal)
    54.         {
    55.             if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
    56.             {
    57.                 facingRight = !facingRight;
    58.                 Vector3 theScale = transform.localScale;
    59.                 theScale.x *= -1;
    60.                 transform.localScale = theScale;
    61.             }
    62.         }
    63.     }

    The float in question is movement speed, and you will find the movement coding under the void handle movement. Thanks in advance.
     
  2. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    30
    Hi @MasterElement, looking at your code, it seems that the issue is inside HandleInput.
    Input.GetKeyDown is returning true only on the first frame when the key is pressed. To fix it, you can either change it to the following:
    Code (CSharp):
    1. private void HandleInput()
    2. {
    3.     if (Input.GetKeyDown(KeyCode.RightShift))
    4.     {
    5.         movementSpeed = 20;
    6.     }
    7.     else if(Input.GetKeyUp(KeyCode.RightShift))
    8.     {
    9.         movementSpeed = 10;
    10.     }
    11. }
    this way, you will reset movement to the lower speed only on the first frame when the key is released.
    You can read more about these methods here https://docs.unity3d.com/Manual/class-InputManager.html and here https://docs.unity3d.com/ScriptReference/Input.html.
    Hope this helps!
     
    MasterElement likes this.
  3. MasterElement

    MasterElement

    Joined:
    Jun 13, 2016
    Posts:
    51
    It works!! Thank you so much. I will read that article, so thanks for that too.
     
    MarekUnity likes this.
unityunity