Hi, I am attempting to create a simple platformer and I am having issues trying to implement the method to allow the player to jump on the heads of enemies.
I am trying to detect collision with an object, “EnemyHeadCollider”. This has its collision box setup right and should be working just fine. However, this object is parented to another object, “Enemy” (I want different results when the player touches the enemy head on, and when the player jumps on top). Every time I attempt to collide with EnemyHeadCollider, even when moved far away from Enemy, it will always detect Enemy as the collided point.
If I detach EnemyHeadCollider, it detects it perfectly as it should, yet I want it to remain attached.
I am handling this with a very simple script on the player:
So I think what you’re thinking is the issue is actually just intended behavior on Unity’s part. So you have 2 colliders in the scene then – an “enemy” and then “enemyHead”. I can’t see how often that debug statement is printing, but I bet it’s a lot? Your head and enemy body are likely colliding with each other. You should set them to separate layers, and then disable collision between those two layers. If you’re unfamiliar with layers it would be easier for you to just google “Unity Physics Layers” than for me to explain.
If that’s not the problem, double check your collider size to make sure the player isn’t actually just hitting the body before it’s hitting the head (if the body is too large).
Anyway, I don’t believe the parenting is the issue in this case, or even related. (I could be wrong on that, but regardless I suspect it’s one of the first two things I mentioned)
“I had the same problem. A way of fixing it is to just changed one of the colliders to a trigger and make the script an ontriggerenter function instead of oncollisionenter. However, if you have more than 2 colliders then this isn’t a way of doing it.”