- Home /
How to know when two specific object colide
hello I want to know how to trigger something when only two specific objects colide. for example when the ball object hits the wall changes to red color and when it hits the floor it turns blue (just for example). any thoughts?
Answer by · Sep 27, 2010 at 08:25 AM
You should take a look at OnCollisionEnter
Using your specific example (in Javascript):
function OnCollisionEnter( collisionInfo : Collision )
{
if ( collisionInfo.gameObject.tag == "wall" )
{
renderer.material.color = Color.red;
}
if ( collisionInfo.gameObject.tag == "floor" )
{
renderer.material.color = Color.blue;
}
}
This script would be attached to your ball, and assumes you have a collider/rigidbody on both objects (and neither are kinematic). You'd also need to create tags for 'wall' and 'floor', and specify it on each object.
Finally, there are plenty of similar questions about OnCollisionEnter. In future, try searching around and having a go on your own first.
Your answer
Follow this Question
Related Questions
OnDestroy collision detect 4 Answers
Collision Detection without a RigidBody 2 Answers
Smooth Camera 2 Answers
Set bounds of collider added at runtime 1 Answer
Collider Error[SOLVED] 2 Answers