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!
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
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)
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… 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.
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!