- Home /
calculate velocity of animated parent rigidbody from script inside child
basically I have a character that needs to change from idle to walk position when it's moving. I've placed it inside an empty object (Character Parent) that I'm moving in the Z axis using animation (so it should have a Z velocity) and have defined the character parent as a rigidbody. Yet Z velocity using Debug.log(rb.velocity.z) shows up as 0. Here is my script, I've tried enabling "Animate Physics" but that didn't work. New to unity so I'm sure this is a simple fix
public class IdleToWalk : MonoBehaviour { public Animator animate; public bool isWalking;
void Start()
{
animate = this.gameObject.GetComponent<Animator>();
}
void Update()
{
Rigidbody rb = GetComponentInParent<Rigidbody>();
if (rb.velocity.z == 0) animate.SetBool("isWalking", false);
else animate.SetBool("isWalking", true);
}
}
Answer by lgarczyn · Jan 18, 2020 at 07:38 PM
Few problems here!
First, you shouldn't move a rigidbody using its transform. Because you are doing that, the rigidbody doesn't realize it is moving, and therefore velocity is always 0.
Your other problem is that your animation speed might not match your actual speed. You could use SetFloat to send the z velocity to your animator, so that you can accelerate the animation when needed.
To move a rigidbody, there's a few different tools:
If it is not kinematic, use rigidbody.velocity or rigidbody.AddForce. For example
rigidbody.velocity = Vector3.forward * Input.GetAxis("Vertical") * playerSpeed;
If it is kinematic, use MovePosition:
rigidbody.MovePosition(rigidbody.position + Vector3.forward * (Input.GetAxis("Vertical") * playerSpeed * Time.fixedDeltaTime));
If you use a CharacterController, use Move or SimpleMove.
Only use transform.position if you don't have a rigidbody or CharacterController.
Only use rigidbody.position if you want to teleport your rigidbody instead of moving it continuously.
Ok so based on your response I think using move or simple move will be the best thing for me since I am using a character controller. However, because I'm making an animation ins$$anonymous$$d of a game, I need the character to move automatically without user input, so am I wrong to use a character controller?
Ps. You're right about my animation not matching the actual speed.
Oh, then it's a different problem.
Animations are meant to move non-colliders, or kinematic rigidbodies with "animate physics" enabled.
What you're doing is closer to a cutscene technically. With an animation taking control of a character ins$$anonymous$$d of user input or AI.
If the animation is always enabled, I would recommend removing any rigidbody and character controller, and simply using a script like this, on Update:
float velocity = (transform.position - prevPos).magnitude / Time.deltaTime;
animator.SetFloat("Speed", velocity);
animator.SetBool("Walking", velocity > 0.001f);
prevPos = transform.position;
with the private var
Vector3 prevPos;
Initialized on OnEnable to transform.position
Answer by mscardinal · Jan 18, 2020 at 06:37 PM
@gitlinjoss I assume your animation moves the character instead of adding a velocity to its rigidbody. So only transform.position.z changes and not rigidbody.velocity.z
Yes that's exactly what happens, transform.position.z is changing but rigidbody.velocity.z isn't. How do I add a velocity to it's rigidbody? Is that possible with animation?
I just realized that rb.velocity is read only so no. But you can set a trigger in the animation then read that from script and add velocity through the script. A bit complicated but possible.
I think what I'll do, since transform.position.z is changing, is create a script that calculates the velocity based on it's change in position. Seems a bit easier. Thanks for the responses man!
no problem but i see some problems youll run into but thats not my problem ;) anyways you know where you can get help
RigidBody.velocity is definitely not readonly.
Your answer
Follow this Question
Related Questions
How to calculate the speed at which a rigidbody2d is actually moving ? 1 Answer
What is wrong with this rigidbody? 2 Answers
Character Joints and Animations results in strange physic animation 1 Answer
When I try to save the velocity of a rigidbody and use it later it doesn't work? 1 Answer
Can't access rigidbody commands 'Rigidbody' does not contain a definition for... 2 Answers