- Home /
Use The Force Luke
I would like to create an effect for my game in which all enemies within a radius of the player are effected. What Might I need to look up in the Documentation to do so?
Answer by ShinAli · Sep 23, 2011 at 01:10 AM
You can accomplish this in several ways.
You can get a list of objects in the scene that would be affected by this (your enemies), and measure the distance between the player and each enemy to see if it would be equal or less to the radius you have defined. You can use the GetObjectsOfType function to get your enemies, but you could also have some sort of manager object in the scene which keeps track of all enemies that are on scene.
Another way you can do this is creating a sphere prefab with a collider and instantiate it where the player is. You can write the OnTriggerEnter functions to check if the object that caused the trigger is an enemy via some tag (or you can have the OnTriggerEnter functions on the enemies and check if the object is your force sphere). You would have to disable rendering of the sphere object, of course.
Unity is an extremely versatile tool for game development but you'd have to think on a lower level than straight game mechanics to script successfully. There can be many other ways to accomplish what you're looking for than the two things I've told you. Just choose or create what's best for what you're trying to do.