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

Question Find Closest Object? (C#)

Discussion in 'Scripting' started by ezequielbfocuus, Jun 6, 2022.

  1. ezequielbfocuus

    ezequielbfocuus

    Joined:
    Aug 17, 2020
    Posts:
    6
    Hello, I need help as I am new into programming and I came up with a problem that I dont know how to solve. This is my idea.

    Suppose there are 3 teams, A, B, and C. Only one character in each team, all AI. I need to determine a way in which each character knows what his closest character from other team is. For example, in a line there are A, then B 2 metters appart, then C 3 metters appart. So closest object for A is B, closest object for B is A, but closest object for C is B. I need this so then I will add a script to follow the closest target, thats not the problem now, the problem is I dont know how can I make it so each character detects closest target independent of the team the closest target is.

    Should I make a blank script for each character, and then another script that everyone has, so that they determine which of the other targets is closest based on the blank script for each character? Maybe I am making it way harder than it is. So, help please.
     
  2. AnonimiAngels

    AnonimiAngels

    Joined:
    Jan 10, 2020
    Posts:
    4
    Vector3.Distance(transform.position, target.position)
    It may be this