- Home /
destroy gameobject when collider is fast enough?
Hey there, i'm searchin for a solution for the following:
I want to destroy a GameObject on collision, but only when the other collider is fast enough.
For example if a car with low speed hits a tree, then the tree should not break. When the car is fast enough the tree should break/destroy.
How can i quantify the speed of a collider?
Hope you guys know what i mean.
Code could look like this:
public float speed;
void OnCollisionEnter(Collision collision){
if (collision.collider.tag = "Car" && speed >= 10)
{
destroy(gameobject);
} else {
debug.log("Not fast enough");
}
}
Sorry for bad english.
Greets
Answer by JoshDangIt · Jul 17, 2016 at 03:21 AM
https://docs.unity3d.com/ScriptReference/Collision-relativeVelocity.html
That should help you.
So, it works good but the problem is, that i can't give my GameObject a non kinematic rigidbody. No Rigidbody or a $$anonymous$$inematic one, this would work but then i can't request the magnitude.
Any other solution for this?
Answer by Zeoli · Jul 16, 2016 at 09:11 PM
The car should have a speed. if it is public you should be able to reference it via collision.gameObject (http://docs.unity3d.com/ScriptReference/Collision-gameObject.html) then all you need to do is... (sorry not at my dev PC right now) but it would be basically something similar to this.
void OnCollisionEnter(Collision collision){
if(collision.gameObject<Car>())
{
if (collision.gameObject<Car>().Speed >= 10)
{
destroy(gameobject);
}
else
{
debug.log("Not fast enough");
}
}
}