- Home /
How can I make an enemy hurt the player?
I am working on a simple prototype where I want to use a trigger box collider to cause damage to the player. The player uses a character controller. This trigger is attached to an enemy that walks around. My intent is to damage the player when it enters this trigger. It just turns out that OnTriggerStay won't fire if the player is standing still (if the enemy walks onto the player). I guess this might seem a bit backward but I'd really want to have a "damage trigger" attached to the enemy just because it feels natural. This won't work because it seems OnTriggerStay won't fire unless the player moved into the collider, not the other way around.
- What do you suggest I'd do instead?
Also, the docs say:
This message is sent to the trigger and the collider that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.
I don't have any rigidbody though, but it surely responds to my character controller. Is it a fault in the docs or am I using OnTriggerStay wrong?
// My damage trigger code, that doesn't work if the player is standing still.
public class DamageTrigger : MonoBehaviour
{
void OnTriggerStay(Collider collider)
{
collider.SendMessage("OnDamage", SendMessageOptions.DontRequireReceiver);
}
}
Below are my status updates:
As suggested, I have tried using OnTriggerEnter and OnTriggerExit but it doesn't help.
// My damage trigger code, that doesn't work if the player is standing still. public class DamageTrigger : MonoBehaviour { List<Collider> colliders = new List<Collider>();
void Update()
{
foreach (var collider in colliders)
{
collider.SendMessage("OnDamage",
SendMessageOptions.DontRequireReceiver);
}
}
void OnTriggerEnter(Collider collider)
{
colliders.Add(collider);
}
void OnTriggerExit(Collider collider)
{
colliders.Remove(collider);
}
}
OnTriggerStay won't fire unless the player moved into the collider, not the other way around. Thank you, I needed to know that for my own project! Never thought of that and it actually is a great deal of help to me! Thank you!
isn't it great when someone elses problem gets fixed, and their problem is your solution? that's when everyone's happy!
It's a matter of perspective, really. Even though your game makes it appear that the enemy is hurting the player, code-wise, the player is suffering from proximity to the enemy... so it makes sense that the trigger code should be player-based. I make that mistake all the time, thinking in terms of "what is happening (from the perspective of the game player)" rather than "what is the game doing". As for the solution, you might have to code a proximity-based backup with a damage-limit (to keep the damage from doubling up)...
"so it makes sense that the trigger code should be player-based" - So, that means that if the enemy stands still (as opposed to the player) then my player wont hurt ins$$anonymous$$d. I think I have got to ditch the trigger idea entirely.
Answer by Statement · Dec 11, 2013 at 12:12 AM
Thought I'd clean up my old questions.
A trigger moving into another trigger must also have a rigidbody.
Any physics object that is moving must have a rigidbody for its callbacks to trigger.
Also, moving objects with a collider (trigger or not) that does not have a rigidbody will incur a performance penalty since Physx will have to rebake the entire scenes static physics data. Colliders without rigidbodies fall into that category.
What I should have done back then was to ensure both moving triggers had a rigidbody on them.
Answer by Dwair · Dec 22, 2010 at 02:56 PM
Have you tried with another approach? don't use OnTriggerStay, but OnTriggerEnter and OnTriggerExit.
When entering the trigger, use SendMessage to tell the player object to change into a "hurting continously" status. When exiting that trigger, use SendMessage to end that "hurting" status.
That way the main code is on player, but the triggers remain useful enough ;)
Actually, this has the same problem. When moving the trigger into a collider, Trigger messages doesn't seem to fire.
interesting... If your trigger seems not to work, try to analyze the situation: There are two possible problems, the trigger isn't working (so you should make sure your player has a collider and your trigger actually looks for collisions in the same layermask) Or the Send$$anonymous$$essage doesn't work, you made sure your Send$$anonymous$$essage is sent to the right gameobject? Debug.Log(); is really useful there :P
Other things to try is that the collider actually tests if the object entering the trigger is actually the player. $$anonymous$$aybe other gameobject is interfering with your game logic.
I saw you say in another post that you're moving the trigger towards your standing player? mmhh, that can be troublesome...try pinpointing the problem with my earlier comment...if anything else fails, you should try to use Physics.Raycast(); it can behave just like a sphere trigger in fact!
Answer by 4s4 · Aug 07, 2012 at 12:25 PM
I bypassed this by calculating the distance between the trigger and player object.
"if the distance is less than collisionbox = triggered"
dunno how efficient, but it works.