• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Im2Silent4You · Jan 22, 2021 at 10:21 AM · gameobjecttransformplayeraitarget

How do I find a player for AI to target when spawning a player in?

So I am trying to make the Ai find a target. I can't set a transform because if I set the character controller to the transform for the target it won't have a target due to when players spawn it it creates them as clones so the Ai doesn't know to target them. How do I set the Ai to target the players via tag?`

 public UnityEngine.AI.NavMeshAgent nma;
 public Transform target;
 public tpEffect[] players;
 PhotonView pv;

 public int lowDamage = 5;
 public int maxDamage = 15;
 float closestDistance = Mathf.Infinity;

 public float findRadius = 10;

 public float range = 150;

 public Texture pfp;


 void Awake()
 {
     pv = GetComponent<PhotonView>();


     if (pv.IsMine)
     {
         pv.RPC("setName", RpcTarget.AllBuffered, "Zombie " + Random.Range(0, 999));
         findNearest();
     }

     nma = GetComponent<UnityEngine.AI.NavMeshAgent>();

     target = GameObject.FindGameObjectWithTag("Player").transform;
 }

 [PunRPC]
 public void setName(string nm)
 {
     gameObject.name = nm;
 }



 void Update()
 {

     players = GameObject.FindObjectsOfType<tpEffect>();

     nma.Resume();

     if (target != null)
     {
         nma.SetDestination(target.position);
         if (target != transform)
         {
             nma.SetDestination(target.position);
         }
         else
         {
             if (pv.IsMine)
             {
                 findNearest();
             }
         }

     }




     if (target == null)
     {
         if (pv.IsMine && players.Length > 0)
         {

             //Debug.Log("Finding");
             findNearest();
         }

     }

     Collider[] hitColliders = Physics.OverlapSphere(transform.position, findRadius);

     foreach (Collider hit in hitColliders)
     {
         if (hit.gameObject.name != "Cube")
         {
             if (hit.gameObject.tag == "Player")
             {
                 if (hit.gameObject.GetComponent<tpEffect>() != null && hit.gameObject.GetComponent<PhotonView>() != null)
                 {
                     if (hit.gameObject.transform != target)
                     {
                         pv.RPC("setTarget", RpcTarget.AllBuffered, hit.gameObject.name);
                     }
                 }
                 else
                 {
                     if (hit.gameObject.GetComponentInParent<tpEffect>() != null && hit.gameObject.GetComponentInParent<PhotonView>() != null)
                     {
                         if (hit.gameObject.transform != target)
                         {
                             pv.RPC("setTarget", RpcTarget.AllBuffered, hit.gameObject.name);
                         }
                     }
                 }
             }
             else
             {
                 if (hit.transform.parent != null)
                 {
                     if (hit.transform.parent.tag == "Player")
                     {
                         if (hit.gameObject.GetComponentInParent<tpEffect>() != null && hit.gameObject.GetComponentInParent<PhotonView>() != null)
                         {
                             if (hit.gameObject.transform.parent != target)
                             {
                                 pv.RPC("setTarget", RpcTarget.AllBuffered, hit.transform.parent.name);
                             }
                         }
                     }
                 }
             }
         }
     }
 }


 public void findNearest()
 {
     closestDistance = 9999;
     if (players.Length > 0)
     {

         foreach (tpEffect pl in players)
         {

             if (Vector3.Distance(transform.position, pl.transform.position) <= closestDistance)
             {
                 if (pl.transform != transform)
                 {
                     if (target != pl.transform)
                     {
                         closestDistance = Vector3.Distance(transform.position, pl.transform.position);
                         //Debug.Log("Loop");
                         pv.RPC("setTarget", RpcTarget.AllBuffered, pl.gameObject.name);
                     }
                 }
             }

         }

         if (target == null)
         {

             pv.RPC("setTarget", RpcTarget.AllBuffered, players[Random.Range(0, players.Length)].name);

         }

     }
 }

 [PunRPC]
 public void setTarget(string name)
 {
     if (GameObject.Find(name) != null && name != gameObject.name)
     {

         target = GameObject.Find(name).transform;

     }
     else
     {
         if (pv.IsMine)
         {
             if (players.Length > 0)
             {
                 pv.RPC("setTarget", RpcTarget.AllBuffered, players[Random.Range(0, players.Length)].gameObject.name);
             }
         }
     }
 }


 void OnTriggerEnter(Collider col)
 {
     if (col.gameObject.GetComponent<tpEffect>() != null)
     {

         pv.RPC("setTarget", RpcTarget.AllBuffered, col.gameObject.name);

     }

 }

} `

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

301 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need assistance with an primitive AI. 0 Answers

Enemy Patrol AI is sideways 0 Answers

Null Reference Exception for target in a script on a newly instantiated object 0 Answers

[Urgent] Finding Nearest Target 2 Answers

How do you convert UnityEngine.GameObject to UnityEngine.Transform? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges