Hi guys,
OnCollisionEnter I am calculating the magnitude to determine whether or not to play a sound clip. Which works fine for most of the collisions, but it is triggering with my ball that is rolling on a plane. Since the ball is moving at a relatively fast speed and the magnitude doesn’t take direction into count, every time it rolls it plays the clip. Sometimes 15 times a second. I only want the clip to play when the ball is dropped on the plane. Does anybody have some suggestions or solutions to calculate the impact velocity? And what is the ImpactForceSum member of collision?
Thanks!
If you subtract the contact point of the collision from the object’s position, you will get a vector specifying the direction of the collision:-
var collisionDir: Vector3 = collision.contacts[0].point - transform.position;
Normalise the direction vector, and then take the dot product between it and the rigidbody’s velocity:-
var inlineSpeed: float = Vector3.Dot(collisionDir.normalized, rigidbody.velocity);
The result is the speed of the object in the direction of the collision. Basically, you just play the sound if this speed is greater than a certain value, which you will have to tweak to suit your game.
2 Likes