- Home /
How to find rigidbody velocity magnitude at the moment it is reflecting off of the other one?
Hello!
Here's the problem.
Let's say there is a ball moving with constant speed 50, so rigidbody.velocity.magnitude is 50 too. At some frame it is bouncing off the surface. I want to check out it's speed on collision, and set speed to 50 if it is lower than 50.
However, because the ball is being reflected back, distance between points of previous and current frame is definitely less than 50.
Just imagine:
point A - ball position in prev. frame,
point B - ball position on collision,
point C - ball position in current frame (bounced back).
Is it any way to get not the distance magnitude between A→C, but the whole A→B→C route?
private Rigidbody rb;
private void OnCollisionEnter(Collision other)
{
if (other.collider.name == "Pad")
{
ContactPoint cPoint = other.contacts[0];
rb.velocity = Vector3.Reflect(rb.velocity, cPoint.normal);
if (rb.velocity.magnitude < 50)
{
rb.velocity = rb.velocity.normalized * 50;
}
}
}
Answer by bdeniz1997 · Mar 15, 2021 at 05:37 AM
you can put that code inside update function or fixed update maybe? they update like every fixed frame. that way it can check if its under 50 even when it's still on air, when it has not collided to anything yet. you dont have to wait it to touch something to check the speed right? cuz thats what you trying to do there.
if you want to capture the speed at collision or like, the speed just before it collides, you can use Raycasting.
that's all i can think of.
Thanks for reply. You right, operations with physics definitely better to move into FixedUpdate.