This code works fine and bounces the projectile, but I would like to know the angle of the projectile to the point/surface it hit so that I can conditionally check if it will really bounce off or penetrate the material. So a projectile that hits the surface in a low angle wont penetrate.
void OnCollisionEnter(Collision collision) {
this.rigidbody.velocity = Vector3.Reflect(this.transform.position, collision.contacts[0].normal);
}
Any suggestions how to get this done?
Are you sure that this code works fine? It should be:
void OnCollisionEnter(Collision collision) {
rigidbody.velocity = Vector3.Reflect(rigidbody.velocity, collision.contacts[0].normal);
}
Anyway, you could measure the angle between the velocity and the normal like this:
public float maxAngle = 95;
void OnCollisionEnter(Collision collision){
Vector3 normal = collision.contacts[0].normal;
Vector3 vel = rigidbody.velocity;
// measure angle
if (Vector3.Angle(vel, -normal) > maxAngle){
// bullet bounces off the surface
rigidbody.velocity = Vector3.Reflect(vel, normal);
} else {
// bullet penetrates the target - apply damage...
Destroy(gameObject); // and destroy the bullet
}
}