I have a weird error
In fighter script I use
using UnityEngine;
using RPG.movement;
using RPG.core;
namespace RPG.combat
{
public class Fighter : MonoBehaviour, IAction // need cancel
{
Transform target;
float timesincelastattack = 0f;
[SerializeField] float Weaponrange = 2f;
[SerializeField] float timebetweenattacks = 1f;
private void Update()
{
timesincelastattack += Time.deltaTime;
if (target == null) return;
if (!GetinRange())
{
GetComponent<Mover>().MoveTo(target.position);
}
else
{
GetComponent<Mover>().Cancel();
//Cancel();
AttackBehaviour();
//print("fight not moving");
}
}
private void AttackBehaviour() // attack animation
{
if (timesincelastattack > timebetweenattacks)
{
timesincelastattack = 0;
GetComponent<Animator>().SetTrigger("attack");
}
}
bool GetinRange()
{
return Vector3.Distance(transform.position, target.position) <= Weaponrange;
}
public void Attack(CombatTarget ctarget)
{
GetComponent<ActionScheduler>().StartAction(this);
// print("attack moving");
target = ctarget.transform;
}
public void Cancel()
{
target = null;
// print("stop attacking (cancel)");
}
void Hit() // attack animation state calls this
{
print("hit called");
if (target == null) return;
Health health = target.GetComponent<Health>();
health.TakeDamage(5);
}
}
}
The player keeps attacking the enemy prefab (original or now prefab) and refuses to stop (as in I can’t move away) (I have an amusing video I’ll upload if needed).
The only way I can stop it is by calling the cancel function to set the target to null. However enemy doesn’t take damage then!