Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
2
Question by Adrian-Salarda · Mar 18, 2016 at 04:27 AM · movementrigidbody2dphysics2daddforcetop-down

How to stop the character moving after the button is pressed, using Rigidbody2D()..AddForce?

Hi guys,

I'm currently making a top-down game that has the player character using physics (Rigidbody2D().AddForce)

You move the player using the WASD keys, however the character keeps moving forward. I want it to stop moving once the button is released. I'm still quite new to C# so some help would be very appreciated. Thanks again :)

Here's my code;

public class PlayerMovement2 : MonoBehaviour {

 public float speed; 
 
 // Update is called once per frame
 void FixedUpdate () { 

     if (Input.GetKey (KeyCode.D)) { 
         GetComponent<Rigidbody2D>().AddForce (Vector2.right * speed); 
     }
         
     if (Input.GetKey (KeyCode.A)) { 
         GetComponent<Rigidbody2D>().AddForce (Vector2.left * speed); 
     } 

     if (Input.GetKey (KeyCode.W)) { 
         GetComponent<Rigidbody2D>().AddForce (Vector2.up * speed); 
     } 

     if (Input.GetKey (KeyCode.S)) { 
         GetComponent<Rigidbody2D>().AddForce (Vector2.down * speed); 
     }
 }

}

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
7

Answer by LazyElephant · Mar 18, 2016 at 05:22 AM

First, in a script like this which will be called frequently, it's good practice to cache your Rigidbody2D in your class so you don't have to call GetComponent so often. In a small game it probably won't make a noticeable difference, but if you do it frequently in all of your scripts, the wasted time can add up. You would do this by adding the following lines to your class.

 public class PlayerMovement2: MonoBehaviour {
      public float speed;
      Rigidbody2D rbody;
 
      void Awake() {
           rbody = GetComponent<Rigidbody2D>();
      }
     //...
 }

As for stopping the player, you could move using the rigidbody's velocity instead of AddForce.

 public class PlayerMovement2: MonoBehaviour {
      public float speed;
      Rigidbody2d rbody;
     
      void Awake() {
           rbody = GetComponent<Rigidbody2d>();
      }
         
      void FixedUpdate() {
           float x = Input.GetAxis("Horizontal");
           float y = Input.GetAxis("Vertical");
           
           rbody.velocity = new Vector2(x*speed, y*speed);
      }
 }

If the player doesn't stop quickly enough using GetAxis, you can go to Edit->Project Settings->Input and change the Gravity under the Horizontal and Vertical axes. The higher the number, the faster the axis will go back to 0 after you let go of the movement key.

Comment
Add comment · Show 3 · 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
avatar image Adrian-Salarda · Mar 19, 2016 at 10:01 AM 0
Share

Thank you so much! It worked very well :)

avatar image LazyElephant Adrian-Salarda · Mar 19, 2016 at 06:11 PM 0
Share

If this is what you wanted, please accept it as the correct answer by clicking the checkmark

avatar image jesxr44 · Jan 15, 2020 at 06:58 AM 0
Share

Thank you very much, it worked. In my case I'm also using AddForce to move the object back and forward faster.

float x = Input.GetAxis("Horizontal"); Vector2 vector = new Vector2(x, 0); rb.AddForce(vector force); rb.velocity = new Vector2(x velocity, 0);

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.

Follow this Question

Answers Answers and Comments

56 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

Related Questions

2D Top-Down Character: How to make my character move using physics? 1 Answer

Objects are sliding when one is placed on top of the other in a top down game 0 Answers

[2D] Can't AddForce to x but y works fine. 1 Answer

RigidBody2D AddForce Not Adding Acceleration 0 Answers

RiigidBody2D.AddForce() isnt working at all. 0 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