Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by itamariuser · Apr 18, 2016 at 02:31 PM · c#2d gamephysics2drigidbody.addforce

How can I make something move without floating after I release the move key?

I'm new to Unity 5 and I've been trying to make a first project which is basically PONG, I got to the part where I want the player to move up when I press W, but the movement is floaty- the player continues to move abit after I release W.

     public KeyCode moveUp;
     public KeyCode moveDown;
     Vector2 movespeed = new Vector2 ();
     private Rigidbody2D rb;
 
     void Start()
     {
         movespeed.x = 0;
         rb = GetComponent<Rigidbody2D> ();
 
     }
     void Update () 
     {
         if (Input.GetKey (moveUp)) {
             movespeed.y = 10;
             rb.AddForce (movespeed);
         }
 
         else if (Input.GetKey (moveDown)) {
             movespeed.y = -10;
             rb.AddForce (movespeed);
         }
         else 
         {
             movespeed.y = 0;
             rb.AddForce (movespeed);
         }
     }

I tried accessing the velocity of RigidBody2D:

 rb.velocity.y = 10;

But it seems to work only in Unity 4.

Is there any way to make the object stop when releasing the move key?

Comment
Add comment · Show 1
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 Le-Pampelmuse · Apr 18, 2016 at 03:04 PM 1
Share

You are moving a physical object with forces. A physical object will never stop abruptly without a counterforce, because it is a slave of inertia.

Either apply a properly calculated counter-force to it when releasing the buttons, or change the approach from physical (rigidbodies and forces) to position-based (position.z += 1) for example.

Setting the rigidbody to kinematic and then enabling physics again on demand is a mis-use of the entire physics system, like $$anonymous$$elv$$anonymous$$ay explained already.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by MelvMay · Apr 18, 2016 at 02:58 PM

 rb.velocity.y = 10;

This is a C# issue, it won't work in Unity 4 either. Velocity is a Vector2 value-type so you cannot set an individual component of it. When accessing it, you get a copy of the value-type (they are passed by-value).

To do this, you need to set the velocity to a whole new Vector2 like so:

 rb.velocity = new Vector2 (rb.velocity.x, 10.0f);

This is how C# works:

  • rb.velocity (returns a copy of the velocity Vector2)

  • rb.velocity.y = 10 (sets the Y of the copy of the velocity Vector2)

If you want to zero the velocity then you'd do this:

 rb.velocity = new Vector2.zero;

Note that by applying force in the 'Update' callback, you're doing it per-frame therefore you'll get different forces and speeds depending on the frame-rate. You should do this in the 'FixedUpdate' callback.

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 itamariuser · Apr 18, 2016 at 03:23 PM 0
Share

It works! Thank you for taking the time to explain!

avatar image MelvMay ♦♦ itamariuser · Apr 18, 2016 at 03:26 PM 0
Share

Good to hear. Good luck!

avatar image Le-Pampelmuse MelvMay ♦♦ · Apr 18, 2016 at 05:18 PM 1
Share

Boom. Just made you a moderator! :P

avatar image
0

Answer by M-Hanssen · Apr 18, 2016 at 02:39 PM

Set rigidbody to kinematic = true when you want to make it stop completely.

Don't forget to switch to kinematic = false when you move it again

Comment
Add comment · Show 1 · 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 MelvMay ♦♦ · Apr 18, 2016 at 02:52 PM 1
Share

Changing the body type like this is non-trivial and is a mis-use. It causes all contacts to be deleted and re-calculated during the next fixed-update.

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

6 People are following this question.

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

Related Questions

i am making a pong game and it works fine till i reset the game 1 Answer

Random freezes during the game 0 Answers

Z position moving in 2D space 1 Answer

Rigidbody.AddForce and Time.TimeScale 1 Answer

I can't seem to rotate my character depending on what side he is running to (2D sidescroller) 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