distance from one enemy and two player

i see and use Vector3.distance ,but it works only for one player ,and it does not calculate the distance of other player ???help

Do you want two different distances, or the average distance?

different, i use a inheritance for the two player……and wrote:

 protected Playertotal players;

public virtual void Init()
   {
      

    players = GameObject.FindGameObjectWithTag("Player").GetComponent<Playertotal>();
   
   }
private void Start()
   {
         Init();
   }
public virtual void movement()
{
float distance = Vector3.Distance(transform.localPosition, players.transform.localPosition);
        //Debug.Log("Distance: " + distance + " for: " + transform.name);
        if (distance > 4.0f)
        {
            Hit = false;
            anim.SetBool("InCombat", false);
        }

This will return all of the objects with the given tag in an array, instead of just one. Then you can calculate the distance for each of the players.

i use the array but it said " ‘GameObject[ ]’ does not contain a definition for ‘transform’ and no accessible extension method ‘transform’ accepting a first argument of type ‘GameObject[ ]’ could be found (are you missing a using directive or an assembly reference?)"

You’re using transform.localPosition. Try using something like:

float distance = Vector3.Distance(transform.position, players.transform.position);

I’m guessing you’re doing something like this:
arrayOfObjects.transform
Instead, you have to get each object out of the array one at a time
arrayOfObjects[0].transform
arrayOfObjects[1].transform
If you aren’t familiar with arrays, I’d suggest going through the Learn section of the website. Arrays will be important as you continue developing.

1 Like

when i declare the array
public GameObject[ ] gos;
and in void start()
{
gos = GameObject.FindGameObjectsWithTag(“Player”);
}
is finished so?
it works with distances but enemy only receive damage,and do not attack