MovePosition Velocity

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;

1 Like

If you’re calling Rigidbody.MovePosition on each FixedUpdate then you can simply read Rigibody.velocity.

Ill have to double check that. Im sure I had that originally but it wasnt correct. Maybe its changed. Maybe im being daft!

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.

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.

What version of Unity are you using?

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

for anyone else that needs the solution…

50 = 1 / time.fixedDeltaTime;

            trackVelocity = (rigidBody.position - lastPos) * 50;
            lastPos = rigidBody.position;
5 Likes

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?

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

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.