MovePosition and IsKinematic causes rigidbody to float away forever

On rare occasions, a character in my game will start to float away… The following code replicates the problem:

var pos = rigidBody.position;
pos.y += 0.01f;
rigidBody.MovePosition(pos);
rigidBody.velocity = Vector2.zero;
rigidBody.isKinematic = true;

Any ideas why? I’m using both MovePosition and isKinematic at different points in my game, so sometimes that will cause a character to float away…

Hi,

I’m bit perplexed. So this example code is what causes problems or is just what the problem looks like?

The code does mostly what I’d expect it to do. I think MovePosition sort of overrides setting velocity directly → Removing MovePosition will stop movement up, otherwise this movement is applied, no matter if you call the MovePosition in Update, LateUpdate or FixedUpdate, MovePosition seems to override setting velocity.zero.

If it would happen inside of Update, it would be no surprise. But a single call to a method containing that code will cause the character to float as well.

The code above shows what the problem looks like. I don’t use that code in 1 method. However, in rare occasions my game will try to apply moveposition and iskinematic to the same character at the same time, which will cause that character to float away forever.

Edit:
So, perhaps an example will help in this case :slight_smile:

Suppose the code above is inside a method called “Test()”, and a UI button will perform “Test()” on button click. In this case, clicking the button once will cause the character to float away forever.

Ah, OK. I haven’t noticed such behavior, but then again I don’t have that much experience scripting Unity Physics.

Check this out: 5.0.2f Rigidbody2D MovePosition setting the velocity now? - Unity Engine - Unity Discussions

I also think that MovePosition does not IMMEDIATELY move the object - it just tells the object how much to move the next time it gets around to moving. Combine this with the bug mentioned in the linked thread, and you have a situation where MovePosition laughs at your attempts to control an object’s velocity manually.