Collision not working between character and a cube!

Hi guys, I have a character which has a character controller for sure and I want it to collide with a cube.

I’m attaching the code that the cubes have as script:

function OnCollisionEnter(collision : Collision) {
	if(collision.gameObject.tag=="Player"){ 
	print("hit");
 }
}

Even though I put this script to the cubes, they cannot print the “hit” message and cannot detect the collision with my character.

BTW, I have untriggered the option “istrigger” in Box Collider of the cubes. Do I need to add some rigidbody to someone? Anyways I wait for your answers! Thanks in advance!

Does the cube has a rigidbody component?

1 Like

No it didn’t have, I added it right now and still cannot print the message, so it’s not colliding with the cube.

When I use OnTriggerEnter it does detect but the character passes through the cube and that’s not what I want :(.

it wont move if it were a trigger.

Yeah I know but my cubes are all untriggered in box collider section, I added a rigidbody component to my character and also to the cubes and still can’t get collision detection. Do you have any idea how to solve this?

I dont know the reason.but I know how to fix it.Put a trigger A as the child object of your player.
and this code attached to the cube.
function OnTriggerEnter(other:Collider)//other would be A
{
if(other.transform.root.tag==“Player”)
{
Debug.Log(“hit”);
}
}
and ontriggerenter needs less calculation than oncollisionEnter

OnTriggerEnter event detects the character but when I use it my character passes through the cube and that’s not what I want.

you dont get it…the player is a still collider it will not pass the cube,but its child object A is a trigger,and A will be detected by the cube!!!

Does it ever enter the OnCollisionEnter?
I’d put the print / debug.log before the if (or stick a break point in there), to eliminate whether it’s something wrong with the if statement, or whether it’s never getting in there.
If it’s the if statement, try using the comparetag function instead.
If(collision.gameObject.CompareTag(“Player”))
{

}

If the rigidbody is moving really quickly / a lot, sometimes you have to check the isKinematic box (although this probably isn’t the desired function for the player)

Yeah I got it, I’ve already done that and still doesn’t work.

That’s my problem, with the OnTrigger event I got this: (character passing through the cube: not what I really want)

@NoirQ Now, I’ve been trying all combination and what works is: Collider on my cube (box collider) and Rigidbody and capsule collider (both) in my main character. But still it really makes my game run reaaaaally slow! And it does print the collision message, but it runs really slow… :frowning: Still won’t get this collision working…

Are you using a CharacterController? I’ve not used them before, but I think the theory is you wouldn’t need the rigidbody on it if you are. I think in that scenario the rigidbody should be on the cube.

Might be worth trying to use OnControllerColliderHit in the player script if you are, see if that gives better results.
http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.OnControllerColliderHit.html

If you aren’t using a CharacterController, maybe try changing the collision on the character to a box collider. It won’t help in the long run, but if it collides correctly, it’ll show it’s something up with the capsule, rather than the script.

Whoaaaah my gosh, it worked! Thanks NoirQ, owe you life! hahaha OnControllerColliderHit works perfectly fine for my character and I don’t even needed to put the rigidbodies, just detects the collider in the cubes and that’s it. Thanks a lot! :smile:

i have same problem

You solve same way?