Hello, I have a script where a enemy must choose the closest player. It works with a single player but not with multiple players. I have more code but I only show you the necessary because if not it would be very long. I used a code that calculates which is the closest player (bestTarget), and the enemy should find him. The error: EnemyController.cs(35,25): error CS1501: No overload for method GetClosestPlayer' takes 0β arguments
Thanks in advance ![]()
void Awake ()
{
nav = GetComponent ();
bestTarget = player = GameObject.FindWithTag (βPlayerβ).transform;
}
void Update ()
{
if(!isDead)
{
GetClosestPlayer ();
nav.SetDestination (player.position);
controller.SetFloat ("speed", Mathf.Abs(nav.velocity.x) + Mathf.Abs (nav.velocity.z));
float distance = Vector3.Distance(transform.position, player.position);
if(distance < 3)
{
attackTimer += Time.deltaTime;
}
else if (attackTimer > 0)
{
attackTimer -= Time.deltaTime*2;
}
else
attackTimer = 0;
}
}
Transform GetClosestPlayer(Transform[]players)
{
float closestDistanceSqr = Mathf.Infinity;
Vector3 currentPosition = transform.position;
foreach (Transform potentialTarget in players)
{
Vector3 directionToTarget = potentialTarget.position - currentPosition;
float dSqrToTarget = directionToTarget.sqrMagnitude;
if(dSqrToTarget < closestDistanceSqr)
{
closestDistanceSqr = dSqrToTarget;
bestTarget = potentialTarget;
}
}
return bestTarget;
}