- Home /
OnTrigger Enter not working - Tried everything!
I have a project in which I am trying to create combat. I have a Player character in which I spawn the weapon as a child of the players hand. In front of my Player is an enemy.
The problem is that the OnTriggerEnter function never fires. I have an animation in which the weapon swings forward in which the 2 Box colliders intersect. I've slowed down this animation significantly as well to make sure this is the case.
I have read through every post I could find and nothing seems to solve it for me.
The Weapon
Has a RigidBody (not 2D)
Has a Box Collider (not 2D)
Is set to "IsTrigger"
Is set to "IsKinematic"
Has a Script attached to it (IAMelee.cs) with the code involved
The Enemy
Has a RigidBody (Not 2D)
Has a Box Collider (Not 2D)
Is NOT set to "IsTrigger"
Is NOT set to "IsKinematic"
The Code in IAMelee.cs
void OnTriggerEnter(Collider other)
{
Debug.Log("Collision");
}
What am I doing wrong?
Did you tried by making the RigidBody of the weapon non kinematic ?
Just to make sure, check if you did not accidentally disable console notifications. (try using debug.log() on a Start() method to see if it's showing on the console)
You seem to do things correctly based on the information available so far. I think it will be hard for us to figure out what is going wrong with only this information. Would you be able to share your project or even better, a $$anonymous$$imal version of it with just things absolutely necessary to reproduce the problem you're having? I'm really curious about what is causing this issue and would like to take a look at the problem.
Answer by Waterpolo7 · Dec 23, 2021 at 09:45 PM
Hi @Mortilicious,
this probably won't change much, but what about changing the collision detection in your rigidbody component to "continuous"? I don't really see anything wrong with your components, though. Another solution (which doesn't contain collision voids) would be using a raycast to check if the enemy is in range (to send damage to their script) and just playing the animation without having a collider on your weapon.
I hope this is somewhat useful, feel free to react :)
Hey @Waterpolo7
I gave it a try, but it doesn't seem to work! I did get a message that a Kinematic Rigidbody can only do speculative continuous, but even then I didn't seem to be able to register anything.
Hi @Mortilicious I can only suggest you using a raycast then. I think the raycast method should also make sure you avoid the collider not registering while it should be hitting, as I don't think colliders are the best option here. If you insist on using the colliders, you could always try the answer by YagirX; checking your layer matrix in Edit > Project Settings > Physics > Layer Matrix, because some layers could be ignoring each other. If some of those are not marked, then you could try marking all of them and see if it helps and if no other bugs appear because of it. Could you also check what layer both objects are on in their inspector window? Let me know what happens :)
Hey @Waterpolo7 Thanks for taking the time to look into it :) Glad to see I'm not the only one who's stumped. My Layers are all set to default, so I'm not sure if that's fine? As I was going through the other answers they all said that using the default layers is the easiest way to get things to work, but in case you need more nuance you can play around with them. Considering that its not working now anyway, I figured I shouldn't look at this. Could you emphasize a bit on how you would approach the Raycast method? I know what Raycasts are, but as far as I know they are used to plot a single trajectory between 2 points?
Answer by YagirX · Dec 24, 2021 at 11:54 AM
Not the fact that it will work, but try to change the type of collision and make the object not kinematic and freeze all the constraints. Perhaps something in the layers was done wrong or fixed Time Step = 0.
Answer by heislerc0 · Dec 28, 2021 at 11:05 PM
Both GameObjects must contain a Collider component. The trigger must have Collider.isTrigger enabled, and contain a Rigidbody.
So your sword is the Trigger. The OnTriggerEnter(Collider other) function should be written in your Enemy's Script.
It is your enemy that enters the trigger (sword), so you must use the "other" parameter to figure out what the triggers is and act accordingly. You can use tags or check for your IAMelee script.
IAMelee sword = other.gameObject.GetComponent<IAMelee>()
Will be null if not the melee object causing the trigger.
other.tag == "melee"
Return true or false, but you have to add the tag to your object with the trigger.
It is perfectly fine to have the OnTriggerEnter function on the game object that has the Collider set to trigger. I use it like that in my game.
It did not work for me like that last time I tried. I would recommend they still try this solution.