Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

MovePosition and IsKinematic causes rigidbody to float away forever

Discussion in 'Physics' started by Rostam24, Sep 15, 2015.

  1. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    On rare occasions, a character in my game will start to float away... The following code replicates the problem:
    Code (CSharp):
    1. var pos = rigidBody.position;
    2. pos.y += 0.01f;
    3. rigidBody.MovePosition(pos);
    4. rigidBody.velocity = Vector2.zero;
    5. 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...
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    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.
     
  3. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    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 :)

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

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Ah, OK. I haven't noticed such behavior, but then again I don't have that much experience scripting Unity Physics.
     
  5. Peez-Machine

    Peez-Machine

    Joined:
    Jul 30, 2013
    Posts:
    27