Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

distance from one enemy and two player

Discussion in 'Scripting' started by michele2000bon, Jan 9, 2020.

  1. michele2000bon

    michele2000bon

    Joined:
    Nov 29, 2019
    Posts:
    40
    i see and use Vector3.distance ,but it works only for one player ,and it does not calculate the distance of other player ????help
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Do you want two different distances, or the average distance?
     
  3. michele2000bon

    michele2000bon

    Joined:
    Nov 29, 2019
    Posts:
    40
    different, i use a inheritance for the two player…..and wrote:
    Code (CSharp):
    1.  protected Playertotal players;
    2.  
    3. public virtual void Init()
    4.    {
    5.      
    6.  
    7.     players = GameObject.FindGameObjectWithTag("Player").GetComponent<Playertotal>();
    8.    
    9.    }
    10. private void Start()
    11.    {
    12.          Init();
    13.    }
    14. public virtual void movement()
    15. {
    16. float distance = Vector3.Distance(transform.localPosition, players.transform.localPosition);
    17.         //Debug.Log("Distance: " + distance + " for: " + transform.name);
    18.         if (distance > 4.0f)
    19.         {
    20.             Hit = false;
    21.             anim.SetBool("InCombat", false);
    22.         }
    23.  
     
    Last edited: Jan 9, 2020
  4. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
  5. michele2000bon

    michele2000bon

    Joined:
    Nov 29, 2019
    Posts:
    40
    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?)"
     
  6. PaulR

    PaulR

    Joined:
    Nov 14, 2012
    Posts:
    43
    You're using transform.localPosition. Try using something like:


    Code (CSharp):
    1. float distance = Vector3.Distance(transform.position, players.transform.position);
    2.  
     
  7. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    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.
     
    michele2000bon likes this.
  8. michele2000bon

    michele2000bon

    Joined:
    Nov 29, 2019
    Posts:
    40
    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
     
    Last edited: Jan 10, 2020