• 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 youngapprentice · Dec 06, 2015 at 02:34 AM · 2dphysicsrigidbody2djump

AddForce90 Jump while moving Rigidbody with MovePosition

Hi, all!

Inside of fixedUpdate, I move my character every frame with Rigidbody2D.MovePosition() and I have a speed variable that is -1 with LeftArrow, 1 with RightArrow, and 0 with no input.

This works great. I'm trying to do an AddForce Jump with Impulse, however, and although the jump function executes, the player doesn't jump. I'm assuming this is because MovePosition is overwriting the Jump. Is there a way around this?

     void FixedUpdate(){
         if (Input.GetKeyDown (KeyCode.UpArrow) && IsGrounded ()) {
             myBody.isKinematic = false;
             myBody.AddForce (new Vector2 (0, jumpForce), ForceMode2D.Impulse);
         }
         //TODO: Player can't jump unless movement is commented out
         myBody.MovePosition (transform.position + (Vector3.right * currentSpeed * Time.fixedDeltaTime));
     }

Thanks! - YA

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 MelvMay · Dec 06, 2015 at 02:35 PM

MovePosition doesn't overwrite anything. It simply calculates the velocity required to move the body to the specified position in the next update. It stores the current velocity first and restores it immediately afterwards.

By applying your force as an impulse, you immediately update the velocity. When you call MovePosition, that velocity will be stored but not used in the next update (the MovePosition velocity will). In the subsequent update, the velocity you modified via the impulse would be used but, from what I can see, you are continually updating the position so any velocity the body has will never, ever be used.

You can't constantly tell a body to move to a position each fixed update and then expect the body velocity to move it somewhere else.

Stop using MovePosition like this. Instead, use AddForce to move the body.

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 youngapprentice · Dec 06, 2015 at 05:52 PM 0
Share

I found my own solution by manually setting the velocity. Using AddForce to move the player would cause the player to gain speed quickly and shoot around.

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

53 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

Related Questions

2d cannon projectile not following expected path 1 Answer

Why/How 2d tower of blocks collapse? 0 Answers

2D Platform irregular jump 1 Answer

Problem with a multi-jump script ( C# ) 1 Answer

2D Physics movement system is creating unexplained random variations. 0 Answers

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