Hey! This problem has been solved, I just can’t get rid of the topic.
Hello! I’m currently working on the Zombie Runner game in the course Complete C# Unity Game Developer 3D. I’m on chapter 166 (trigger animation in code). The problem I’m having is that once I add the animations (Idle, move, attack)) the enemy stops from moving. It only looks at the player but doesn’t chase it like it did before I added the animations.
Here are some screenshots:
When the game starts, the character plays it’s idle animation. When I get close enough to the enemy the animation switches to “Move” and stops the idle animation. Once it’s at move it doesn’t actually move. It only rotates where the player is located at.
HERE COMES MY ENEMY AI CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
[SerializeField] Transform target;
[SerializeField] float chaseRange = 5f;
NavMeshAgent navMeshAgent;
float distanceToTarget = Mathf.Infinity;
bool isProvoked = false;
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
distanceToTarget = Vector3.Distance(target.position, transform.position);
if (isProvoked)
{
EngageTarget();
}
else if (distanceToTarget <= chaseRange)
{
isProvoked = true;
}
}
private void EngageTarget()
{
if (distanceToTarget >= navMeshAgent.stoppingDistance)
{
ChaseTarget();
}
if (distanceToTarget <= navMeshAgent.stoppingDistance)
{
AttackTarget();
}
}
void ChaseTarget()
{
GetComponent<Animator>().SetTrigger("move");
navMeshAgent.SetDestination(target.position);
}
void AttackTarget()
{
Debug.Log("Attacked!");
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, chaseRange);
}
}
Here’s the enemy just looking at me when I come close.
I really appriciate if someone could help me, I’m really struggeling hah, thanks for your time!