using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooter : MonoBehaviour
{
[SerializeField] GameObject projectile, Weapon;
AttackerSpawner myLaneSpawner;
Animator animator;
Attacker attacker;
[SerializeField] float Range;
private void Start()
{
SetLaneSpawner();
animator = GetComponent<Animator>();
attacker = FindObjectOfType<Attacker>();
}
private void Update()
{
if (IsAttackerInLane())
{
animator.SetBool("IsAttacking", true);
}
else
{
animator.SetBool("IsAttacking", false);
}
}
private void SetLaneSpawner()
{
AttackerSpawner[] attackerSpawners = FindObjectsOfType<AttackerSpawner>();
foreach (AttackerSpawner spawner in attackerSpawners)
{
bool IsCloseEnough = (Mathf.Abs(spawner.transform.position.y - transform.position.y) <= Mathf.Epsilon);
if (IsCloseEnough)
{
myLaneSpawner = spawner;
}
}
}
private bool IsAttackerInLane()
{
var attackerPos = attacker.transform.position.x;
if (myLaneSpawner.transform.childCount <= 0 &&
attackerPos - gameObject.transform.position.x > Range)
{
return false;
}
else
{
return true;
}
}
public void Fire()
{
Instantiate(projectile, Weapon.transform.position, Weapon.transform.rotation) ;
}
}
This is from the glitch garden section of the 2d unity course
I want to implement range because i have some defenders that can only shoot at close range(eg knight)
Pls tell me what i did wrong! Im a beginner