- Home /
enemy aggro region definitive answer
hi ive been looking around for a good solution for implementing a region around an enemy, that the player must enter to aggro the enemy and begin the enemys attack function.
Ive seen stuff with trigger colliders on a child object of the enemy and using sendmessage. ive seen a ray suggestion.
I want to know, what is the proper way of implementing something like this in unity. thanks.
For my particular enemy, i want it to charge in a straight line directly at the point of aggression, i dont know how that would change the implementation or not...
Answer by whydoidoit · Jul 10, 2012 at 01:31 PM
So Raycasts are quite expensive, so normally you would only do them if a range check or Trigger indicated the then enemy was likely to be able to attack. If your enemy's desire to attack is based on line of sight then you will need to do a raycast or maybe a capsule cast to see if the player is reachable. Doing that for all enemies all of the time is a waste though, so I would combine it with a collider as that at least let's you visualise the area of attack.
hmm thats a little confusing for me. I was thinking of making a box trigger around the enemy using an empty gameObject child as the trigger container(the enemy itself has a standard collider). I could size it so that it extends only to the sides, not up or down, to prevent trigger collisions if the player is above or below the enemy.
If the player enters the trigger, a ray could be shot to that direction and if theres contact with the "player" tag, then the attack function is run.
Does this sound like a proper solution? and if it is, then how do shoot a ray to where the player collided with trigger? Can i do something like:
if trigger entered by player
ray direction = player position - enemy position; //Would this work? im not expert on vectors
shoot one ray in ray direction
if hit gameobject tag = player do attack function?
Yes that's the right idea - but you might use a capsule cast to ensure that the enemy can actually make it to the player.
function OnTriggerEnter(other: Collider)
{
if(other.gameObject.tag == "player") {
var hit : RaycastHit;
if(Physics.Raycast(transform.position, other.gameObject.transform.position - transform.position, 1000, hit)) {
if(hit.gameObject.tag == "player") {
//do attack
}
}
}
hey thanks a lot for the code, will save me good time. Its okay if the enemy can't make it, it will create humorous situations :)
but for future reference though just so i understand, a capsule cast is like ins$$anonymous$$d of shooting a single tiny ray, it shoots an actual capsule? like a ball shaped object(deter$$anonymous$$ed by parameters of course, preferably sized to the enemy gameobject)
Yes that is right - it shoots something to show whether a character can actually make it (through potentially narrow spaces) - rays are just a line - but a capsule represents the actual size of the body. Can be very helpful to avoid "strange" situations!
Okay, here's the issue I'm having. I have the sphere trigger collider as a child object of my foe character, but the OnTriggerEnter is only being called when I actually collide with the character controller, and the collider is being ignored, even though the collider and the script are both on the child object, and the character controller is on the parent object.
In short, how do I specify WHICH trigger, since it seems to think the trigger I'm referencing is the controller.