x


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?

more ▼

asked Sep 27 '10 at 07:55 AM

raminsh gravatar image

raminsh
173 15 15 21

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Sep 27 '10 at 08:25 AM

Marowi gravatar image

Marowi
4.9k 4 14 53

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2501
x2090
x1705
x322

asked: Sep 27 '10 at 07:55 AM

Seen: 1282 times

Last Updated: Sep 27 '10 at 08:20 AM