I have an moving enemy which has a big weapon on him to attack the player.
I want to place a trigger on his weapon, and when the trigger touches the player, it starts an attack script (with OnTriggerEnter).
The problem is that when the player moves into the trigger, it works, but when the player stands still and the trigger moves into the player, it doesn’t.
The trigger weapon has a box collider and the player has a character controller.
I find this pretty strange. Has anybody had any experience with this?
And is there an easy solution to it? Or a workaround?
Does one or other of the objects (trigger or player) have a rigidbody attached? Generally, this is necessary to make triggers work. You should probably have the isKinematic option set on the rigidbody in this particular case.
If it is a matter of the rigidbody being asleep, you can send out messages on swing for the player.ridigbody.WakeUp(), which will prepare the player rigidbody for collision.
It sounds as though it doesn’t swing unless it triggers the trigger. So you would probably have to check the distance from the weapon to the player, and after it dips below a certain distance, then wake up the rigidbody.
The sleep state of a rigidbody is an optimization that should not affect the simulation. And should not require manual intervention.
I’m not sure if triggers should activate (or trigger) when moving into rigidbodies, but its arguably something you would expect.’
EDIT: (From manual)
If you have a sleeping rigidbody and you move a static collider (A collider without a Rigidbody attached) into the rigidbody or pull it from underneath the rigidbody, the sleeping rigidbody will not awake.
@BogdanDude, as andeee said, kinematic will do for you.