MovePosition on a single axis, retain velocity on the other
Hey all,
I'm using MovePosition to move a crate with the player when pushing/pulling. This works great.
The issue is that when applying the new X position, the current Y position is also applied, causing it to create janky y axis behaviour.
pushableRigidbody.MovePosition (new Vector2( pushableTargetX, pushableRigidbody.position.y ) );
Is there any way to only use MovePosition on the x axis only?
Have you tried storing the y position in a variable before calling $$anonymous$$ovePosition then using this value in the y component of the Vector2?
Answer by MelvMay · Nov 06, 2015 at 09:07 AM
By passing in the existing body.y position, this won't change the Y position at all. MovePosition simply calculates the velocity required to move the Rigidbody2D in a single physics update, it doesn't directly alter position.
Also know that if you're using Rigidbody2D interpolation then the Rigidbody2D position will be different than the Transform position (transform is interpolated each frame).
Presumably you're not calling this each frame because it would be pointless as MovePosition only works during the physics update (FixedUpdate).
You might want to try dumping the Rigidbody2D position and the Transform position prior to calling MovePosition to check what you're asking it to do.
Thanks for the reply. In this particular instance, I was able to solve the issue by not using $$anonymous$$ovePosition when the crate was in a state which required inherited y velocity.
Your answer
Follow this Question
Related Questions
How can I isolate just the character to slow down 0 Answers
Player Dips Into Ground After Jump 0 Answers
problem with overlapsphere 1 Answer
Adding Inertia after rotation 0 Answers
i need help making this code work with 2D physics. 0 Answers