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
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 minimal 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.
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.
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.
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.
Did you tried by making the RigidBody of the weapon non kinematic ?
– NPnpNPnpNPJust 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)
– LaikenYou 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 minimal 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.
– endasil_unity