I have a cube chasing me in my game. I have some water in my game.
I would like it so when the cube collides with the water (daylight simple water), it carries on for chasing for 2 seconds then destroys itself.
I don’t have any code at the moment. I would really appreciate it if it were actual code apposed to a tutorial.
give a tag to the water, the word “water” is just fine. In the class of the cube put a variable
bool collisionTIme;
then check the collision
void OnCollisionEnter (Collider target) {
if( target.gameObject.tag.Equals("water") == true ){
collisionTime = Time.time;
}
}
void OnCollisionStay (Collider target) {
if( target.gameObject.tag.Equals("water") == true ){
if( Time.time - collisionTime > 2 ){
Destroy(gameObject);
}
}
}
}
You should use OnCollisionEnter: Unity - Scripting API: Collider.OnCollisionEnter(Collision)
And if you want to make it with specific object, try something like
function OnCollisionEnter (hit : Collision)
{
if(hit.transform.gameObject.name == "name")
{
//do stuff
}
}