• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Seanthebeast300 · Nov 24, 2022 at 05:25 PM · movementmovement scripttopdownmomentum

Top Down Player Keeps Speed After Key Is Released

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?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public float moveSpeed;
     public Rigidbody2D rb;
     private Vector2 movenput;
 
     public GameObject SliderUp;
     public GameObject SliderDown;
     public GameObject SliderRight;
     public GameObject SliderLeft;
     public float decreaseSpeed;
     public float regainSpeed;
 
     private void RegainStam()
     {
         if (SliderRight.transform.localScale.x < 2.6f)
         {
             if (Input.GetKey("right") == false)
             {
                 SliderRight.transform.localScale += new Vector3(regainSpeed, 0, 0);
             }
         }
 
         if (SliderLeft.transform.localScale.x < 2.6f)
         {
             if (Input.GetKey("left") == false)
             {
                 SliderLeft.transform.localScale += new Vector3(regainSpeed, 0, 0);
             }
         }
         if (SliderUp.transform.localScale.x < 2.6f)
         {
             if (Input.GetKey("up") == false)
             {
                 SliderUp.transform.localScale += new Vector3(regainSpeed, 0, 0);
             }
         }
         if (SliderDown.transform.localScale.x < 2.6f)
         {
             if (Input.GetKey("down") == false)
             {
                 SliderDown.transform.localScale += new Vector3(regainSpeed, 0, 0);
             }
         }
     }
     private void StopMovement()
     {
 
 
         if (Input.GetKey("right"))
         {
             movenput.x = Input.GetAxisRaw("Horizontal");
             
             if (SliderRight.transform.localScale.x <= 0f)
             {
                 movenput -= new Vector2(1, 0);
             } 
            
         }
         if (Input.GetKey("left"))
         {
             movenput.x = Input.GetAxisRaw("Horizontal");
             if (SliderLeft.transform.localScale.x <= 0f)
             {
                 movenput += new Vector2(1, 0);
             }
         }
         if (Input.GetKey("down"))
         {
             movenput.y = Input.GetAxisRaw("Vertical");
             if (SliderDown.transform.localScale.x <= 0f)
             {
                 movenput += new Vector2(0, 1);
             }
         }
         if (Input.GetKey("up"))
         {
             movenput.y = Input.GetAxisRaw("Vertical");
             if (SliderUp.transform.localScale.x <= 0f)
             {
                 movenput -= new Vector2(0, 1);
             }
         }
     }
 
     void Start()
     {
 
     }
 
     void Update()
     { 
         
    
         movenput.Normalize();
 
         rb.velocity = movenput * moveSpeed;
 
         RegainStam();
         StopMovement();
 
         if (Input.GetKey("right"))
         {
             if (SliderRight.transform.localScale.x >= 0.0001f)
             {
                 SliderRight.transform.localScale -= new Vector3(decreaseSpeed, 0, 0);
             }
 
         }
         if (Input.GetKey("left"))
         {
             if (SliderLeft.transform.localScale.x >= 0.0001f)
             {
                 SliderLeft.transform.localScale -= new Vector3(decreaseSpeed, 0, 0);
             }
 
         }
         if (Input.GetKey("down"))
         {
             if (SliderDown.transform.localScale.x >= 0.0001f)
             {
                 SliderDown.transform.localScale -= new Vector3(decreaseSpeed, 0, 0);
             }
 
         }
         if (Input.GetKey("up"))
         {
             if (SliderUp.transform.localScale.x >= 0.0001f)
             {
                 SliderUp.transform.localScale -= new Vector3(decreaseSpeed, 0, 0);
             }
 
         }
     }
 
 
 }
 
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by logicandchaos · Nov 24, 2022 at 05:29 PM

You can stop your player when you let go of a key using Input.GetKeyUp, I t$$anonymous$$nk that will work for you.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

212 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Momentum with Character Controller 1 Answer

2D Topdown Character Contoller 0 Answers

How to drag a game object with a mouse (along x axis)? 1 Answer

How do I stop my character from moving in a circle upon destination arrival? 1 Answer

Top down 2d movement without being affected by rotation 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges