Getting an odd issue with the Enemy AI/Animation.
When the player is within range they work perfectly and will follow the player unit, attacking when they are close enough.
When they are out of the follow distance they seem to just stutter on the spot, looking at the Animator the attack and stopAttack trigger is glitching like crazy.
As far as I can tell my code is correct.
using UnityEngine;
using RPG.Combat;
namespace RPG.Control
{
public class AIController : MonoBehaviour
{
[SerializeField] float chaseDistance = 5f;
Fighter fighter;
GameObject player;
private void Start()
{
fighter = GetComponent<Fighter>();
player = GameObject.FindWithTag("Player");
}
private void Update()
{
if (InAttackRangeOfPlayer() && fighter.CanAttack(player))
{
fighter.Attack(player);
}
else
{
fighter.Cancel();
}
}
private bool InAttackRangeOfPlayer()
{
float distanceToPlayer = Vector3.Distance(player.transform.position, transform.position);
return distanceToPlayer < chaseDistance;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Movement;
using RPG.Core;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour, IAction
{
[SerializeField] float weaponRange = 2f;
[SerializeField] float timeBetweenAttacks = 1f;
[SerializeField] float weaponDamage = 5f;
Health target;
float timeSinceLastAttack = 0;
private void Update()
{
timeSinceLastAttack += Time.deltaTime;
if (target == null) { return; }
if (target.IsDead()) { return; }
// tutorial screenshot has if (!GetIsInRange())
if (!GetIsInRange())
{
GetComponent<Mover>().MoveTo(target.transform.position);
}
else
{
GetComponent<Mover>().Cancel();
AttackBehaviour();
}
}
private void AttackBehaviour()
{
transform.LookAt(target.transform.position);
if (timeSinceLastAttack > timeBetweenAttacks)
{
// This will trigger the Hit() (animation) event below.
TriggerAttack();
timeSinceLastAttack = 0;
}
}
private void TriggerAttack()
{
GetComponent<Animator>().ResetTrigger("stopAttack");
GetComponent<Animator>().SetTrigger("attack");
}
// 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(GameObject combatTarget)
{
if (combatTarget == null) { return false; }
Health targetToTest = combatTarget.GetComponent<Health>();
return targetToTest != null && !targetToTest.IsDead();
}
public void Attack(GameObject combatTarget)
{
GetComponent<ActionScheduler>().StartAction(this);
target = combatTarget.GetComponent<Health>();
}
public void Cancel()
{
StopAttack();
target = null;
}
private void StopAttack()
{
GetComponent<Animator>().SetTrigger("attack");
GetComponent<Animator>().SetTrigger("stopAttack");
}
}
}
using UnityEngine;
using RPG.Movement;
using System;
using RPG.Combat;
namespace RPG.Control
{
public class PlayerController : MonoBehaviour
{
private void Update()
{
if (InteractWithCombat()) return;
if (InteractWithMovement()) return;
}
private bool InteractWithCombat()
{
RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
foreach (RaycastHit hit in hits)
{
CombatTarget target = hit.transform.GetComponent<CombatTarget>();
if (target == null) continue;
if (!GetComponent<Fighter>().CanAttack(target.gameObject))
{
continue;
}
if (Input.GetMouseButtonDown(0))
{
GetComponent<Fighter>().Attack(target.gameObject);
}
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);
}
}
}
I have made sure Enemy has the scripts
Mover, Health, Fighter, Action Scheduler, Combat Target and AI controller.
Player has
Mover, Health, Player Controller, Fighter and Action Scheduler
I’m sure it’s something simple I am overlooking but I’ve been looking at it for hours.
Originally they weren’t moving at all but realised I had forgot to add Action Scheduler and Fighter to my Enemy prefab.
Any help would be appreciated.