- Home /
MAke an enemy attack and look at other enemies HELP?
i made an enemy that look at the player and attack the player but i want to know how to make an enemy that can attack and look at other enemies...the other enemies are taged as ''enemy'' so....?
Your look at and attack code should be the same, just be able to accept a new target. Then, if you want your enemy to attack the closest player or enemy, just find a list of the nearest guys using Physics.OverlapSphere and pick the closest one as the target.
Answer by DESTRUKTORR · Jul 24, 2012 at 05:52 PM
Personally, I find tags to be a bit sloppy, for this very reason, and because, quite frankly, I don't know how much processing power it takes to search through them (or what extra functionality that I don't need that it does anyway).
When you're scripting, you should just have them cycle through everything with the tags for the player, and enemies (and anything else you want them to aggro to), and pick them, accordingly, based on aggro.
For instance, if you want them to prioritize attacking the other enemies, over the player, have them switch to the closest one.
Essentially, I'd go about it starting with a list (dictionaries work, too, but I prefer lists, when I don't need the extra "finding power"), and cycle through all the GameObjects tagged with the various tags you wanted to use, and adding all of them to the list (potentially skipping "dead" ones, or "friendlies" for those enemies), then choosing a target to face and attack, based on weights that you want to use.
Just off the top of my head, some "aggro weighting" methods might use distance (attacking the closest enemy/player, first), damage amounts/influence (attacking the enemy/player that has dealt the most damage/healing/buffing/debuffing/etc.), or specific types of enemies/players might get higher weight, automatically.
For the best gameplay experience, I'd suggest using some combination of these things. If you need help with scripting this, just leave a comment and I'll edit in some code to show you how I'd go about doing it.
Layers and Layer$$anonymous$$asks will help make it easier to use physics-based detectors ins$$anonymous$$d of tags. This means your enemies could be on the "Enemy" layer and then have tags that help to define more properties, or even have an Enemy component to request the data for the "aggro weighting."
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to trigger a Script from an Object. 0 Answers
Trigger to make another trigger to appear? 2 Answers
What’s wrong with my Safe Volume (safe zone) code? 0 Answers
enemy spawn and in game time counter 1 Answer