Hi, i have a simple player and enemy object. The enemy will be able to destroy the player on contact; but for now im just making it print to the console. As the name suggests, it isnt detecting the collisions, it just ignores it. I’ve followed this code exact from a tutorial, but it still doesn’t work (could be outdated). Both objects have is trigger unchecked. Can anyone see anything wrong with it:
void OnCollisionEnter (Collision other) {
print ("Collision");
if (other.transform.tag == "Enemy") {
print ("I Hit Enemy");
}
}
So many things can be wrong
First check that at least one of the objects has a rigidbody on them. Make sure the rigidbody is not set to kinematic and make sure it is on the same transform as the collider.
Second , make sure the layers aren’t ignoring each other by going to Edit / project settings / physics
Both objects have rigidbodies, the collider is in the same location as the object, and they aren’t in kinematic . I am also not using layers at my stage. (so i guess everything is on the same level, right?).
There’re some additional tips from my experience :
• If the collision is quick, more often than not, I had to make the collision box a little bit bigger than what I need (visually) for the collision to be recognized. Seems like, sometimes, the OnCollisionEnter isn’t recognized while OnCollisionStay is.
• For any Mesh Collider, make sure they are convex. Otherwise, while they might be recognized for real-time collision (bounce and halt), they won’t be recognized by OnCollisionEnter.
Also, depending on what you wish to do with your collision. If it’s projectile (like a bullet), I would suggest to use a trigger instead of a standard collision for the projectile. OnTriggerEnter is actually a lot more liable than OnCollisionEnter since OnTriggerEnter is called at the first frame from which the collider is in contact (or inside) the trigger collider’s zone while OnCollisionEnter can be missed as the collider bounce (force away) push the colliders away too fast for there to have the collision recognized.
If you wish to have a standard collider and a hit-zone on the (Enemies for instance), you could use 2 colliders : One mesh or basic collider for the bounce/halt and 1 trigger collider that extend slightly further from the basic collider. Then by using OnTriggerEnter, you won’t have any problem (in theory if you set things right).
This only applies if the Collider’s “IsTrigger” field is set.
Plus, this thread was created 5 years ago; pretty sure the OP isn’t having this problem anymore.
i had the same problem but i found the solution (at least for me).
in the rigid body component of one of the colliding game objects you have to change the setting of “collision detection” from “discrete” to “continuous”.