Edit. I was able to solve the problem by changing spawner’s transform to 0. This way I was able to use Mathf.Abs instead of Mathf.Floor. Feel kinda stupid but hey, atleast it works now
Feel free to lock the topic.
Hi!
For some reason, all my Defenders are shooting at the same time. It seems that even if only one lane has enemies on it, every instance of the defenders will star shooting. The code is underneath this message:
Please note that i used Mathf.Floor instead of Mathf.Abs because my defenders would not line up correctly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Shooter : MonoBehaviour
{[SerializeField] GameObject projectile; 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.Floor(spawner.transform.position.y - transform.position.y) <= Mathf.Epsilon); if (IsCloseEnough) { myLaneSpawner = spawner; } } } private bool IsAttackerInLane() { if (myLaneSpawner.transform.childCount <= 0) { return false; } else { return true; } } public void Fire() { Instantiate(projectile, transform.position, transform.rotation); }
}