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