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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can i get ANY random transform on a certain layer?

Discussion in 'Scripting' started by jackson31, Sep 8, 2010.

  1. jackson31

    jackson31

    Joined:
    Aug 25, 2010
    Posts:
    28
    Yeah, just wanting to know if it's possible to somehow get a random transform from a certain layer.

    i need this for my AI enemies, so that they can chose to look at and chase after any of the AI on another team and vice-versa.
     
  2. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    Something like
    Code (csharp):
    1.  
    2. var allAIs : Array() = FindObjectsOfType(AI);
    3. target = allAIs[ parseInt( Random.value * (allAIs.length-1) ) ];
    4.  
    ?
    You might want to create your 'allAIs' or "allRedTeamAIs' or whatever arrays at Start(), then just update them whenever you add/remove an AI to avoid calling the expensive FindObjectsOfType function all the time.
     
  3. jackson31

    jackson31

    Joined:
    Aug 25, 2010
    Posts:
    28
    cool thanks, that looks pretty good.

    i also found this function;
    Code (csharp):
    1. targetAI = GameObject.FindWithTag("Red_AIs").transform
    I'll give em both a go :)