Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

MovePosition with interpolation without the Rigidbody being kinematic?

Discussion in 'Physics' started by Development-Boldly-XR, Feb 25, 2020.

  1. Development-Boldly-XR

    Development-Boldly-XR

    Joined:
    Mar 8, 2019
    Posts:
    12
    I'm trying to use interpolation on a Rigidbody controller I'm building, but it works when isKinematic is true. This however causes the player to go trough obstacles. Is it possible to use MovePosition and use interpolation without Kinematic being true?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    For 2D yes but for 3D no. As the docs state:

     
    Forty_Hippos and viesc123 like this.
  3. Development-Boldly-XR

    Development-Boldly-XR

    Joined:
    Mar 8, 2019
    Posts:
    12
    @MelvMay That was indeed what I found, what is the best alternative so that I can use interpolation and
    isKinematic = false;
    ? Preferably something similar like _rb.MovePosition (either the player is moving, or not).
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Well don't use MovePosition and calculate the move yourself after all, MovePosition is doing just that. Calculate the velocity required to move in the physics time-step, set it then during the simulation step it'll move to the position.
     
  5. Development-Boldly-XR

    Development-Boldly-XR

    Joined:
    Mar 8, 2019
    Posts:
    12
    The docs state that it's not recommended to modify the velocity directly and what do you mean with:
    Code (CSharp):
    1. void FixedUpdate () {
    2.     float mH = Input.GetAxis ("Horizontal");
    3.     float mV = Input.GetAxis ("Vertical");
    4.     rb.velocity = new Vector3 (mH * speed, rb.velocity.y, mV * speed);
    5. }
    Would it be possible to interpolate this movement?
     
    Last edited: Feb 25, 2020
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    It's also not recommended to try to move a dynamic body to a specific position but that's what you want to do. Modifying the velocity directly, if not careful, means you can stomp over stuff like gravity, hence it not being recommended if you don't know what you're doing. This is the reason. Adding a impulse does exactly that though but it's adding an impulse to the velocity. But if you don't want to do it, then that's fine too.

    It's already done for you when you select interpolate on a Rigidbody so there's nothing to do. If this is the code in your character controller then I'm not sure where the MovePosition stuff comes in.
     
    Xahell likes this.
  7. Development-Boldly-XR

    Development-Boldly-XR

    Joined:
    Mar 8, 2019
    Posts:
    12
    When trying to move a rigidbody player it is not dynamic correct? Do you mean modifying for example a cube with a rigidbody and boxcollider that is falling from mid-air?

    Yes I want to make use of interpolation, so my rigidbody currently has interpolate set to interpolate. I was indeed using body.MovePosition since I find it alot in tutorials, but since I don't want a kinematic Rigidbody this is not an option. The code shown above is something I found. Another example would be from this post, would interpolation work when handling the movement like this?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    • Calculate the vector from the position to move to from the current rigidbody position (moveVector = transform.position - rigidbody.position)
    • Calculate the velocity to move by this over the next simulation time-step. If this is the fixed-update theb its "velocity = moveVector / Time.fixedDeltaTime".
    • This is the velocity to set the RB to get it to move.
    • Note: Rigidbody linear drag will stop it from getting to the position exactly so either ignore that or set it to zero.
    But yeah, you're trying to get a dynamic body to move with a specific velocity so that it steps to the position you want. I have absolutely no idea WHY you want to do that but it was your request so the above is how you do it.

    Doing this requires that you know what you're doing. If you don't or are not comfortable with the above the use a Kinematic body anddetect collisions using physics queries or simply don't base you movement on moving to specific positions.
     
    asfdfdfd likes this.
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Yes, interpolation works for all cases of the body moving through space via its velocity which is the normal condition that any dynamic body moves.

    3D physics is pretty terrible when it comes to distinguishing body-types. After all, it only has the "IsKinematic" flag so all a fair few devs know is the Kinematic body-type. You see a lot devs saying "non-kinematic" too which is vague.

    For 2D, there's a body-type right there on the Rigidbody2D.
    For 3D, no Rigidbody means static (you should not move the collider). Rigidbody with IsKinematic set means kinematic (you should use MovePosition and no forces/gravity etc are used so no col-det). Rigidbody with IsKinematic not set means Dynamic. Has col-det, reacts to forces and you should not try to set its position explicitly.
     
  10. Development-Boldly-XR

    Development-Boldly-XR

    Joined:
    Mar 8, 2019
    Posts:
    12
    Hi @MelvMay,

    I'm currently using the following code to move the player and the Rigidbody has interpolation set to interpolate. IsKinematic is unticked and after some rough and dirty tests it seems to do the trick. I'm using Cinemachine Freelook to pan around the player and interpolation seems to be working like a charm even at very high fps with v-sync turned off.
    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         float horizontal = InputManager.MainHorizontal();
    4.         float vertical = InputManager.MainVertical();
    5.         rb.velocity = new Vector3(horizontal * speed, rb.velocity.y, vertical * speed);
    6.     }
    I'm not sure what you mean with this? Isn't this kind off doing the same as using rb.MovePosition but now it has the ability to interpolate with IsKinematic unticked? On a side-note when I was using MovePosition IsKinematic was also unticked. Is this bad practice, or am I missing something?
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Err, yes because you asked for it when you said ...
    This is all we've been talking about from the start so I'm completely confused why you would ask this. I think you're missing some fundamental understanding here and missing all my points.

    Not sure I can help you further.
     
  12. Development-Boldly-XR

    Development-Boldly-XR

    Joined:
    Mar 8, 2019
    Posts:
    12
    I was referring to this. The reason for not wanting to use MovePosition is isKinematic has to be ticked for interpolation to work.

    I'm asking this since alot of tutorials and stuff you find online seem to completely ignore the isKinematic part when using rb.MovePosition. This is presumably because when the rigidbody is kinematic collision stops working with other objects in the scene that only have a collider.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    This is fine and all information I already obviously know but you're not following or responding to the actual information I'm giving you so it seems, despite my best efforts, I cannot help you.
     
    Last edited: Feb 25, 2020
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    I'm not confused because of why you came here to ask the question! I'm confused because I've given you the answer to this above and it's been the PRIMARY topic of our conversion here but you then ask it again which is why I'm confused. It just seems you're not following.

    Anyway, you will find all the information in my posts. I can only suggest for now re-reading them.
     
    Last edited: Feb 26, 2020