• 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
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.

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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Random freezes during the game 0 Answers

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

Do I need Rigid2DBody if I don't plan to use physics? 2 Answers

Randomize text position for 2D Quiz C# 0 Answers

stick-figure piercing 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges