So after working through all of it now when i click on the enemy the player just runs up to them and tries to run through them never actually triggering any animations.
my enemy has a capsule collider, the fighter, combat target, health, action scheduler scripts
not sure where in my code it broke.
heres the fighter script
public class Fighter : MonoBehaviour, IAction
{
[SerializeField] float weaponRange = 2f;
[SerializeField] float timeBetweenAttacks = 1f;
[SerializeField] float weaponDamage = 5f;
private Mover moving;
private Animator animator;
private Health target;
float timeSinceLastAttack = 0;
private void Start()
{
moving = GetComponent<Mover>();
animator = GetComponent<Animator>();
}
private void Update()
{
timeSinceLastAttack += Time.deltaTime;
if (target == null) return;
if (target.IsDead()) return;
if (!GetIsInRange())
{
moving.MoveTo(target.transform.position);
}
else
{
moving.Cancel();
AttackBehaviour();
}
}
private void AttackBehaviour()
{
//rotate the player to look at target
transform.LookAt(target.transform);
if (timeSinceLastAttack > timeBetweenAttacks)
{
//This will trigger the Hit() event
TriggerAttack();
timeSinceLastAttack = 0;
}
}
private void TriggerAttack()
{
animator.ResetTrigger("StopAttack");
animator.SetTrigger("Attack");
}
//Animation Event
private void Hit()
{
if(target == null)
{
return;
}
target.TakeDamage(weaponDamage);
}
private bool GetIsInRange()
{
return Vector3.Distance(transform.position, target.transform.position) < weaponRange;
}
//can we attack or is the target dead?
public bool CanAttack(CombatTarget combatTarget)
{
if (target == 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>();
}
//cancel attack animation
public void Cancel()
{
StopAttack();
target = null;
}
private void StopAttack()
{
animator.ResetTrigger("Attack");
animator.SetTrigger("StopAttack");
}
}
heres the player controller script
public class PlayerController : MonoBehaviour
{
private void Update()
{
if (InteractWithCombat()) return;
if (InteractWithMovement()) return;
print("Nothing selected");
}
private bool InteractWithCombat()
{
RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
foreach (RaycastHit hit in hits)
{
CombatTarget target = hit.transform.GetComponent<CombatTarget>();
if (!GetComponent<Fighter>().CanAttack(target))
{
//continue means move on with foreach loop
continue;
}
if (Input.GetMouseButtonDown(0))
{
GetComponent<Fighter>().Attack(target);
}
return true;
}
return false;
}
private bool InteractWithMovement()
{
RaycastHit hit;
bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
if (hasHit)
{
if (Input.GetMouseButton(0))
{
GetComponent<Mover>().StartMoveAction(hit.point);
}
return true;
}
return false;
}
private static Ray GetMouseRay()
{
return Camera.main.ScreenPointToRay(Input.mousePosition);
}
}