I have the same code as in the lecture, but I still get the code wherein the stopAttack trigger isnt getting reset. What seems to be amiss?
My code:
Fighter.cs
using RPG.Movement;
using UnityEngine;
using RPG.Core;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour, IAction
{
[SerializeField] float weaponRange = 2f;
[SerializeField] float timeBetweenAttacks = 1f;
[SerializeField] int minWeaponDamage = 3;
[SerializeField] int maxWeaponDamage = 15;
Health target;
int weaponDamage;
float timeSinceLastAttack = 0f;
private void Update()
{
timeSinceLastAttack += Time.deltaTime;
if(target == null) return;
if(target.IsDead()) return;
if (!GetIsInRange())
{
GetComponent<Mover>().MoveTo(target.transform.position);
}
else
{
GetComponent<Mover>().Cancel();
AttackBehaviour();
}
weaponDamage = Random.Range(minWeaponDamage, maxWeaponDamage);
}
private void AttackBehaviour()
{
transform.LookAt(target.transform);
if(timeSinceLastAttack > timeBetweenAttacks)
{
//This will trigger the Hit event
TriggerAttack();
timeSinceLastAttack = 0;
}
}
private void TriggerAttack()
{
GetComponent<Animator>().SetTrigger("attack");
GetComponent<Animator>().ResetTrigger("stopAttack");
}
//This is an animation event
void Hit()
{
if(target == null) return;
target.TakeDamage(weaponDamage);
}
private bool GetIsInRange()
{
return Vector3.Distance(transform.position, target.transform.position) < weaponRange;
}
public bool CanAttack(CombatTarget combatTarget)
{
if(combatTarget == null) return false;
Health targetToTest = combatTarget.GetComponent<Health>();
return targetToTest != null && !targetToTest.IsDead();
}
public void Attack(CombatTarget combatTarget)
{
GetComponent<ActionScheduler>().StartAction(this);
target = combatTarget.GetComponent<Health>();
}
public void Cancel()
{
StopAttack();
target = null;
}
private void StopAttack()
{
GetComponent<Animator>().SetTrigger("stopAttack");
GetComponent<Animator>().ResetTrigger("attack");
}
}
}