Hi,
My defender bot is transitioning to attack state and staying there even though there is no enemy in lane.
using UnityEngine;
public class Shooter : MonoBehaviour
{
[SerializeField] GameObject projectile;
[SerializeField] GameObject gun;
AttackerSpawner myLaneSpawner;
Animator animator;
private void Start()
{
SetLaneSpawner();
animator = GetComponent<Animator>();
}
private void Update()
{
if(IsAttackerInLane())
{
animator.SetBool("isAttacking", true);
}
else
{
animator.SetBool("isAttacking", false);
}
}
private void SetLaneSpawner()
{
AttackerSpawner[] spawners = FindObjectsOfType<AttackerSpawner>();
foreach (AttackerSpawner spawner in spawners)
{
bool IsCloseEnough =
(Mathf.Abs(spawner.transform.position.y - transform.position.y)
<= Mathf.Epsilon);
{
myLaneSpawner = spawner;
return; // Exit the loop once a valid spawner is found
}
}
// If no valid spawner is found, log a warning3
Debug.LogWarning("No spawner found in the same lane as the shooter." + transform.position.y);
}
private bool IsAttackerInLane()
{
if (myLaneSpawner.transform.childCount <= 0)
{
return false;
}
else
{
return true;
}
}
public void Fire()
{
// Instantiate the projectile
GameObject newProjectile = Instantiate(projectile, gun.transform.position, Quaternion.identity);
// Leave the rotation unchanged to make the projectile move straight
newProjectile.transform.rotation = Quaternion.Euler(0, 0, 90);
}
}