Collision detection - Empty game object

Hi,

I am newbi to unity. I am finding it very powerful and just working my way through it.

I want to detect a collision between an empty game object (which is moving) and a cube (which is static). What is the best way of doing this? How do you put a message on the screen that the collision has happened?

thanks
tomcat

you can give an empty game object any of the colliders,
as found in the component → physics menu

here’s a quick setup:

Make a cube, and in that same physics menu, give it a rigidbody.
Create an empty game object below it and give it a sphere collider
Put this script on the cube, in a script called “s_collide” (important, since it will be looking for that name later):

var collided = false;  //initializes a variable we can refer to later


function OnCollisionEnter(){ // when this collides with another collider...

	collided = true;	// set our variable true.
	
}

To display text you’ll need to use GUI elements, so we’ll need to set that up on the camera.

There are a few ways of doing this, including broadcasting / sending messages in a hierarchy, but I used object reference here.

Put this script on the camera:

var cube: GameObject;  // this lets us drag an object to reference.
private var cube_script; 

function Start() {
       //the following will give us access to that 'collided' variable
       // by giving us all the other script's info in a variable.

	cube_script = cube.GetComponent(s_collide); 
	
}

// All of your GUI elements need to be placed in this function,
// such as text on the screen, boxes, or buttons:
function OnGUI() {

//here we look at our other script's "collided" variable:
if (cube_script.collided)
	GUI.Label(Rect(20,20,200,20), "Collision Detected!");
        // a label is basic, non-interactive text.
	
else
	GUI.Label(Rect(20,20,200,20), "Nothing is Happening.");
	
	
}

in order to actually get the camera code to work after you put it on the camera, you’ll need to select the camera, and notice where it has the variable “cube” in the inspector, and there’s an empty spot for a game object. Drag the cube from your object list into that spot.

This will tell the script to refer to that game object.

Now if you place your cube over the empty object in 3D space, it will fall onto it and detect a collision, the camera will notice the collision, and create a label accordingly.

These scripts actually allow the cube to move due to gravity (which can be turned off in the rigid body), and the game object is static, because it was simpler for me to set up.

Sometimes I miss the easy way of doing things, so i apologize in advance if I’ve done that here…

1 Like

Ok, this is what I have done.

I made the cube a rigid body and added this script to it:

var collided = false;  //initializes a variable we can refer to later


function OnCollisionEnter()
{ // when this collides with another collider...

   collided = true;   // set our variable true.
   //GUI.Label(Rect(20,20,200,20), "Collision Detected!"); 
}

function OnGUI () 
{
//GUI.Box (Rect (10,10,100,90), "Collision detector");
	if( collided == true)
		GUI.Box(Rect (10,100,150,100), "Collision detected");

}

Then I added a sphere collider to my empty object. My empty object has a camera as child and is used to move around the game.

Then when I run the game, I get the “Collision detected” before I am anywhere near it. Any ideas what is happening here?

thanks
tomact

This is strange. Because the cube is a rigid body and has gravity, as soon as you run the game, it detects a collision ( I guess with floor), but it never detects the collision with my game object as I hit it. What am I doing wrong?

tomcat

If you’re still using the same code and always hit the floor first, you won’t see a change on screen. You set the variable true on the first collision and then nothing changes. If you really want to test, have it do this:

function OnCollisionEnter(hit : Collision)
{
   Debug.Log(hit.transform.name);
}

Ok, it proved my theory as it returns “plane” which is what my cube collides with.

There must be a way of detecting collision between rigid bodies. I must be missing something really simple :frowning:

tomcat

You should share the project. It’ll make debugging easier.

Thanks, here is the scene I am using and the script that I have attached. Just open it in a standard project and you would be able to see it. I must be missing something very simple !!!

tomcat

175772–6284–$collision_detection_848.zip (6.42 KB)

The scripts are creating a GUI.Box inside Update. Doesn’t that pop up an error for you?

And my original message still applies. After you hit one object, there’s nothing to make it mention that it hit the other object.

OK, not sure if I understand it. I just want to detect a collision when my object hits the cube. What do I need to do?

tomcat

I had the same problem… With a mesh or box collision works fine, but when I use it with an empty game object, it doesn’t work.

Try adding a rigidbody to the empty object, and set its isKinematic flag to true.

1 Like