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

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

Rigidbody projectile misses collider 1 Answer

Adding velocity to an object in a brick breaker game 0 Answers

2d cannon projectile not following expected path 1 Answer

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

Addforce for 2d game is not doing anything. 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