Fighter.cs
using UnityEngine;
using RPG.Movement;
using RPG.Core;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour, IAction
{
[SerializeField] float weaponRange = 2f;
Transform target;
private void Update()
{
if (target == null) return;
if (!GetIsInRange())
{
GetComponent<Mover>().MoveTo(target.position);
}
else
{
GetComponent<Mover>().Cancel();
}
}
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;
}
}
}
namespace RPG.Core
{
public interface IAction
{
void Cancel();
}
}
using UnityEngine;
namespace RPG.Core
{
public class ActionScheduler : MonoBehaviour
{
//אנו קוראים לחוזה שלנו
IAction currentAction;
public void StartAction(IAction action)
{
if (currentAction == action) return;
if (currentAction != null)
{
currentAction.Cancel();
}
currentAction = action;
}
}
}
I don’t know why but my Char wont cancel the attack