- Home /
The question is answered, right answer was accepted
Collision with specific object...
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.
Answer by CupOfMayo · Sep 02, 2013 at 10:42 AM
You should use OnCollisionEnter: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
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
}
}
Hey, i want my camera (box collider non triggered) to collide with walls having a specif tag and non triggered not with any other game object
Answer by thegdeveloper · Sep 02, 2013 at 11:13 AM
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);
}
}
}
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Change gameObject of the VAR TARGET HELP!!! 1 Answer
Sphere detection system 2 Answers
dont instantiate clones 1 Answer
Detection if a GameObject is below you or next to you? 1 Answer