Need help with OnControllerColliderHit

Im new to unity and im trying to use OnControllerColliderHit but it doesnt seem to be working

Here is my script

function OnControllerColliderhit (hit : ControllerColliderHit)
{
      if (hit.GameObject.tag == cube)
      {
      print ("hitting cube");
      }
}

but it is not printing anything =(

Anyone?

i’m not 100% sure, but maybe change the

if (hit.GameObject.tag == cube)

to

if (hit.collider.gameObject.tag == cube)

or just gameObject in your line, not GameObject

that’s just my guess though.

Maybe you should try putting “” around cube. (“” is for String).

oo didn’t notice that - yeah what Lime said :stuck_out_tongue:

i think:

if(hit.gameObject.tag == "cube")

is right :slight_smile:

And then immerhart came and put the puzzle pieces togehter :slight_smile:

+achievement

Nope, Still doesn’t work =(

is the cube tagged?

Yes the Cube is tagged and my character and the cube have a Character Controller attached to them and they are touching each other but its not printing anything.

Ahem, I tried copying your script and it said to me that I had some spelling wrong(a.k.a. it didn’t have any color in it’s name). So I switch the name of the function and copied the one from the scripting refferences. here’s my result. (If the name doesn’t have any color, or it just doesn’t work, then do as I did)

function OnControllerColliderHit (hit : ControllerColliderHit)
{
	print("HEYHEY");
      if (hit.gameObject.tag == "cube")
      {
      print ("hitting cube");
      }
}

where I copied the name of the function:

This actually gave me prints.

just try using triggers instead.
http://technology.blurst.com/unity-physics-trigger-collider-examples/

Ok Ill try using triggers but I dont understand why controller hit is working. Maybe im doing something wrong. Is the script working for you Lime_x?

Are both, the player and the cube have to have Character Controllers or dont you need character controller? and does the player have to be touching the Character controller attached to the cube or does it have to be touching the Cube it self rather than the controller attached to it?

Yes it’s fully functional. the way I posted it.
I didn’t have any rigidbodies on neither of the cubes. I did make sure so that the function was the one from unity. And I also made sure so that the tag was right.
If the tag was wrong and the function worked, then it would print “HEYHEY”. But for me, both the function worked and the tag was right. (the one that I had tagged was the one that didn’t have this script). Have you made sure that the function is the right one(the one copied from the unity link)?

see if i just get two cubes, stick rigid body on “cube”, and on the thing it’s hitting tick “is trigger” in Box collider, then add this script to the ‘thing’ that’s being hit, then the following script works perfectly well:

function OnTriggerEnter (hit)
{
	print("HEYHEY");
      if (hit.gameObject.tag == "cube")
      {
      print ("hitting cube");
      }
}

Actually I tried your method too Lime, out of interest, and i can’t get what you have working either! So now i’m intrigued.

This is getting scarier and scarier. Have you made sure that you copied the one from the link that I posted?

Yeah copy/paste.

How did you test it? Did you move the cube whilst it was playing with the transform or through a controller?

Also, just wondering why not just use OnCollisionEnter(collision : Collision) … why the need for controller?

Ah yes, now I remember. OnControllerColliderHit only works while the cube is moving(so If you don’t have gravity, and it’s standing still on the ground), then it wont move and it wont recognize the hit. I think that might be the problem!