Problem with state animation

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);
}

}

I don’t know what course this is but you’re not checking for enemies in the lane, your checking for anything in the lane. You’re also checking only the last lane that Unity can find. Your code in SetLaneSpawner loops through all the lanes and always sets the myLaneSpawner so after that call, the myLaneSpawner holds the last lane it looked at. The math for IsCloseEnough is pointless because you calculate it, but don’t use it. I think you are missing a if (IsCloseEnough) in there

"Hi there, there is a problem with the FIRE() animation event. I’m struggling to find a solution; all lane defenders are attacking. I only want the lane defender where an enemy spawn to fire but every defender is firing.

Did you fix the issue I mentioned? That ‘enemy’ looks like it may be in the last lane, so all the defenders are looking at that lane and finding an enemy.


Edit
Ok, wait. I was wrong (but it’s still the same issue): The SetLaneSpawner is setting the first lane for all the defenders, not the last. All the defenders are looking at the same lane, so all of them are finding the same enemy

Your SetLaneSpawner should probably look something like this:

private void SetLaneSpawner()
{
    AttackerSpawner[] spawners = FindObjectsOfType<AttackerSpawner>();
    foreach (AttackerSpawner spawner in spawners)
    {
        bool IsCloseEnough = Mathf.Approximately(spawner.transform.position.y - transform.position.y, 0f)
        if (IsCloseEnough)
        {
            myLaneSpawner = spawner;
            return; // Exit the loop once a valid spawner is found
        }
    }

    // If no valid spawner is found, log a warning
    Debug.LogWarning("No spawner found in the same lane as the shooter." + transform.position.y);
}

It is throwing null reference error now. :frowning:

Got it Thank you so much. I was to do something with enemy spawner parent coordinates.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms