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 Velocity

Discussion in 'Physics' started by JamesLeeNZ, Aug 11, 2016.

  1. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Anyone have a confirmed way of calculating velocity when MovePosition is used?

    I thought I had mine setup correctly, but I noticed that projectiles being shot ahead of my tanks last night.

    No code on me, but if you show me yours, ill show you mine later (or I may remote into my box shortly and grab a copy)

    I suspect it should be LastPos-NextPos * ?? (a value based on fixedTimeStep) - thinking it might be 1-FixedTimeStep * 100;
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Ill have to double check that. Im sure I had that originally but it wasnt correct. Maybe its changed. Maybe im being daft!
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    It's a confirmed way. I applied it in one of my projects last week. Note that I'm assuming a kinematic rigidbody! I don't know if/how it works if the rigidbody is non-kinematic.
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I think thats possibly why I thought it didnt work. I might have had them setup as kinematic once upon a time. Been a LONG time since I played with the scripts on my tanks.
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    What version of Unity are you using?

    5.4 definitely does not report velocity for me if I use MovePosition
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    for anyone else that needs the solution...

    50 = 1 / time.fixedDeltaTime;

    Code (csharp):
    1.  
    2.             trackVelocity = (rigidBody.position - lastPos) * 50;
    3.             lastPos = rigidBody.position;
    4.  
     
    wethings, Benfont, MrTruth0 and 2 others like this.
  8. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    I'm on 5.4 right now, and both rb.velocity and rb.angularVelocity get reported properly when using MovePosition and MoveRotation on each FixedUpdate. Maybe calling MoveRotation is also a requirement?
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Nah dont think so as my tanks use movepos/moverot for movement.. My projectiles are accurate with the velocity calculation I used in the previous post.

    Think it was all a misunderstanding about using kinematic vs non-kinematic
     
  10. KevinCastejon

    KevinCastejon

    Joined:
    Aug 10, 2021
    Posts:
    88
    sorry to necro this but just in case anyone is confused by that behaviour, it looks like Rigidbody is indeed updating the velocity even when kinematic, but Rigidbody2D does not.