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.

Question Top Down Player Keeps Speed After Key Is Released

Discussion in 'Editor & General Support' started by Seanthebeast300, Nov 24, 2022.

  1. Seanthebeast300

    Seanthebeast300

    Joined:
    May 30, 2022
    Posts:
    2
    I'm working on a top down game that is similar to the combat in Undertale. The twist is the player runs out of stamina when they go in a certain direction for long enough. I copied a top down movement script from online, and did my best to manipulate it to make the player stop moving when they run out of stamina for that certain direction. I've run into a problem where the player will keep their momentum if the player just taps a certain direction. I'm not sure if the problem has to do with my movement script or the way my rigidbody is set up. My player is dynamic, has a gravity scale of 0, and is freezed on the z axis.

    So I guess the main question is:

    How do I make the player stop moving when that direction isn't being inputted?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float moveSpeed;
    8.     public Rigidbody2D rb;
    9.     private Vector2 movenput;
    10.  
    11.     public GameObject SliderUp;
    12.     public GameObject SliderDown;
    13.     public GameObject SliderRight;
    14.     public GameObject SliderLeft;
    15.     public float decreaseSpeed;
    16.     public float regainSpeed;
    17.  
    18.     private void RegainStam()
    19.     {
    20.         if (SliderRight.transform.localScale.x < 2.6f)
    21.         {
    22.             if (Input.GetKey("right") == false)
    23.             {
    24.                 SliderRight.transform.localScale += new Vector3(regainSpeed, 0, 0);
    25.             }
    26.         }
    27.  
    28.         if (SliderLeft.transform.localScale.x < 2.6f)
    29.         {
    30.             if (Input.GetKey("left") == false)
    31.             {
    32.                 SliderLeft.transform.localScale += new Vector3(regainSpeed, 0, 0);
    33.             }
    34.         }
    35.         if (SliderUp.transform.localScale.x < 2.6f)
    36.         {
    37.             if (Input.GetKey("up") == false)
    38.             {
    39.                 SliderUp.transform.localScale += new Vector3(regainSpeed, 0, 0);
    40.             }
    41.         }
    42.         if (SliderDown.transform.localScale.x < 2.6f)
    43.         {
    44.             if (Input.GetKey("down") == false)
    45.             {
    46.                 SliderDown.transform.localScale += new Vector3(regainSpeed, 0, 0);
    47.             }
    48.         }
    49.     }
    50.     private void StopMovement()
    51.     {
    52.  
    53.  
    54.         if (Input.GetKey("right"))
    55.         {
    56.             movenput.x = Input.GetAxisRaw("Horizontal");
    57.            
    58.             if (SliderRight.transform.localScale.x <= 0f)
    59.             {
    60.                 movenput -= new Vector2(1, 0);
    61.             }
    62.          
    63.         }
    64.         if (Input.GetKey("left"))
    65.         {
    66.             movenput.x = Input.GetAxisRaw("Horizontal");
    67.             if (SliderLeft.transform.localScale.x <= 0f)
    68.             {
    69.                 movenput += new Vector2(1, 0);
    70.             }
    71.         }
    72.         if (Input.GetKey("down"))
    73.         {
    74.             movenput.y = Input.GetAxisRaw("Vertical");
    75.             if (SliderDown.transform.localScale.x <= 0f)
    76.             {
    77.                 movenput += new Vector2(0, 1);
    78.             }
    79.         }
    80.         if (Input.GetKey("up"))
    81.         {
    82.             movenput.y = Input.GetAxisRaw("Vertical");
    83.             if (SliderUp.transform.localScale.x <= 0f)
    84.             {
    85.                 movenput -= new Vector2(0, 1);
    86.             }
    87.         }
    88.     }
    89.  
    90.     void Start()
    91.     {
    92.  
    93.     }
    94.  
    95.     void Update()
    96.     {
    97.        
    98.  
    99.         movenput.Normalize();
    100.  
    101.         rb.velocity = movenput * moveSpeed;
    102.  
    103.         RegainStam();
    104.         StopMovement();
    105.  
    106.         if (Input.GetKey("right"))
    107.         {
    108.             if (SliderRight.transform.localScale.x >= 0.0001f)
    109.             {
    110.                 SliderRight.transform.localScale -= new Vector3(decreaseSpeed, 0, 0);
    111.             }
    112.  
    113.         }
    114.         if (Input.GetKey("left"))
    115.         {
    116.             if (SliderLeft.transform.localScale.x >= 0.0001f)
    117.             {
    118.                 SliderLeft.transform.localScale -= new Vector3(decreaseSpeed, 0, 0);
    119.             }
    120.  
    121.         }
    122.         if (Input.GetKey("down"))
    123.         {
    124.             if (SliderDown.transform.localScale.x >= 0.0001f)
    125.             {
    126.                 SliderDown.transform.localScale -= new Vector3(decreaseSpeed, 0, 0);
    127.             }
    128.  
    129.         }
    130.         if (Input.GetKey("up"))
    131.         {
    132.             if (SliderUp.transform.localScale.x >= 0.0001f)
    133.             {
    134.                 SliderUp.transform.localScale -= new Vector3(decreaseSpeed, 0, 0);
    135.             }
    136.  
    137.         }
    138.     }
    139.  
    140.  
    141. }
    142.  
    143.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,380
    In your
    StopMovement()
    function notice you are only ever changing
    movenput
    when keys are pressed. You don't do anything when keys are released.