When i test the game/script in unity, it moves to the target and stop with correct range that it is supposed to, start attacking and then when i click to move away it keeps attacking. Also the throttle for my attacks doesn’t affect it, im unsure if i missed something.
Also i have been adding everything addil by hand, when i follow in VSC, i dont have the light bulb and certain commands dont work to prefill for my code.
using UnityEngine;
using RPG.Movement;
using RPG.Core;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour, IAction
{
[SerializeField] float weaponRange = 2f;
[SerializeField] float timeBetweenAttacks = 1f;
Transform target;
float timeSinceLastAttack = 0;
private void Update()
{
timeSinceLastAttack += Time.deltaTime;
if (target == null) return;
if (!GetIsInRange())
{
GetComponent<Mover>().MoveTo(target.position);
}
else
{
GetComponent<Mover>().Cancel();
AttackBehaviour();
}
}
private void AttackBehaviour()
{
if (timeSinceLastAttack > timeBetweenAttacks)
{
GetComponent<Animator>().SetTrigger("attack");
timeSinceLastAttack = 0;
}
}
private bool GetIsInRange()
{
return Vector3.Distance(transform.position, target.position) <weaponRange;
}
public void Attack(CombatTarget combatTarget)
{
GetComponent<ActionScheduler>().StartAction(this);
target = combatTarget.transform;
}
public void Cancel()
{
target = null;
}
//Animation Event
void Hit()
{
}
}
}