- Home /
How does rigidbody.velocity work for animated objects?
I have an animated actor, including a sword. That sword has a collider and a rigid body attached, as well as a script which is supposed to determine the relative velocity of the two rigid bodies involved (the sword and a target rigidbody I have defined). The velocity is determined OnCollisionEnter. The problem is that the velocity always seems to be 0 (both relative velocity and velocity of the sword rigid body). If that is actually normal for animated objects, how can I find out the velocity (or even better point velocity) of an animated object? The velocity is not linear, by the way. I'm grateful for any help here!
You need to tick animatePhysics
on the animation component. It calculates rigidbody physics from an animation.
Thank you for the quick reply! The actor has an animator component, but the issue is still there, even with animatePhysics ticked. Oh and funny enough, the only thing that occasionally seems to have a point velocity at the contact point is the (motionless) target. The relative velocity is still 0 though...
Answer by Benproductions1 · Aug 01, 2013 at 10:51 PM
Hello,
If you want to manually calculate the velocity, you can do thing:
//Note this will only work if animatePhysics is not on
var previousPosition:Vector3;
function Update() {
previousPosition = transform.position;
}
function LateUpdate() {
rigidbody.velocity = (previousPosition - transform.position)/Time.deltaTime;
}
It basically calculates the current velocity from positional change per frame.
This can't be done manually because there is no literal LateFixedUpdate.
Hope this helps,
Benproductions1
I think you just want to store the velocity in some variable to use it later, not actually update the rigidbody velocity (which may conflict with the animator).
If you Animate Physics and want to use FixedUpdate
ins$$anonymous$$d, as there is no "LateFixedUpdate", you can still update previousPosition
just after storing the velocity.
Or you can set animation mode to "Animate Physics" and then velocity will be computed on the rigidbodies for you. I personally think that's much cleaner.
Answer by julkiewicz · Mar 01, 2018 at 09:17 AM
It will work, if you set animation mode to Animate Physics on Animator.