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 account, 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?
CodePudding user response:
Consider trying to check if the object's velocity is in your threshold, you could narrow down the collisions checked via using object tags.
More info about rigidbody velocity.
*edit: spelling
CodePudding user response:
You can calculate the impact angle between the surface normal and the relative impact velocity and take it into account.
void OnCollisionEnter(Collision collision)
{
Vector3 velocity = collision.relativeVelocity;
Vector3 normal = collision.GetContac(0).normal;
if (velocity.magnitude > 2)//impact faster than 2
{
if (Vector3.Angle(normal, velocity) > 60)//impact angle greater than 60
{
audioSource.Play();
}
}
}