- Home /
Finding transform.position of Object with specific Tag
Heyo chaps
I've got my code for my 'enemy' working nicely. There will be lots of these enemies in 2D space and in order to prevent them overlapping each other I want them to move away when they're within a certain range. This all works using my current code but currently I manually need to plug in ONE target in the inspector and as such it will avoid this target only.
What I want is to replace the public Transform Target;
with 'GameObject.FindWithTag'
so I can just use a universal tag of 'enemy' and they will all avoid each other but I'm not 100% sure how to implement it and where to carry it through in the rest of my code - being a bit of a novice to programming.
Every method I've tried so far has led to errors throughout the entire code so any direction would be much appreciated!!
using UnityEngine;
using System.Collections;
public class AvoidAllies : MonoBehaviour
{
public Transform Target;
public float speed = 5f;
private float DodgeDistance = 3f;
private float range;
public float dirNum;
void Update()
{
range = Vector2.Distance(transform.position, Target.position);
if (range < DodgeDistance)
{
transform.position = Vector2.MoveTowards(transform.position, Target.position, -1 * speed * Time.deltaTime);
}
}
}
There are ways to do this, but I think it will collapse easily. First of all, FindWithTag returns only one active object,what you need is FindGameObjectsWithTag which returns GameObject[], so you'd have to change Transform to GameObject[] and refactor the code to reflect that. Also Vector2.$$anonymous$$oveTowards as it is now will just try to move to the opposite direction if the distance is lower than a threshold. Even if you changed the above code to gather the rest of the enemies into an array and iterated through it, you'd probably hit some edge cases where you've checked for Enemy A who is towards direction A but some iterations later , the dodging gets you to move towards Enemy A again, and since you iterate and check through an array it won't help you.
You should move the code above into a $$anonymous$$anager of sorts who tries to calculate distances from everyone and try to figure out a direction which is valid for all other enemies. For example find the directions where an enemy is below the dodging distance and try to find a direction which takes into account all of the above directions.
You make a fair point. Seems like there's no real easy way to get through this without encountering (as you said) a pretty detrimental collapse. I'm thinking I'll make a few different versions of the enemy, each with a different distance they have to keep from the main player. Even if it's just a small difference, it'll be enough to remove the jarring visual effect of all the enemies converging on a single point. Also the player will be destroying these enemies enough that it shouldn't be a major problem anyway.
Thanks for shedding some light!
If you haven't already done so, you may want to look into steering behaviours. You can find some information here http://gamedevelopment.tutsplus.com/series/understanding-steering-behaviors--gamedev-12732 or with a quick google search of the topic.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# transform position 1 Answer
Unity Camera and transform? 1 Answer
C# change an object tag wit raycasthit. 2 Answers