My object must explode when the player touches it or when it collides at a low speed.
The problem I'm having is that its speed becomes zero when the floor collides for example and then the OnCollisionEnter
event is called, it is already late and never fulfills the condition.
Someone knows what I'm doing wrong or what I should do to fix this?
void OnCollisionEnter(Collision other)
{
Debug.Log(Mathf.Abs(rb.velocity.magnitude));
if (Mathf.Abs(rb.velocity.magnitude) >= detonationImpactVelocity)
Explosion(200);
if (other.gameObject.tag == "Player")
Explosion (600);
}
I came up with this but I guess there must be a better way to do it.
void Update()
{
if (Mathf.Abs(rb.velocity.magnitude) >= detonationImpactVelocity)
explosionSpeedAchieved = true;
else
explosionSpeedAchieved = false;
Debug.Log(Mathf.Abs(rb.velocity.magnitude));
}
void OnCollisionEnter(Collision other)
{
Debug.Log(Mathf.Abs(rb.velocity.magnitude));
if (explosionSpeedAchieved)
Explosion(200);
if (other.gameObject.tag == "Player")
Explosion (600);
}