Hello, everyone!
I made a similar thread earlier today in the Scripting forum, but I’m guessing this might be a better place to ask.
I have my level’s navmesh set up and ready, but the navmesh agent that’s supposed to chase the player won’t walk around obstacles and gets stuck just walking against the walls.
It’s a bit long, but below is the script that’s attached to the navmesh agent.
I can’t for the life of me figure out what’s wrong with it, and since other than this problem my game is pretty much finished not being able to solve the issue is super frustrating.
Thank you so much for your help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class StrangerBehaviour : MonoBehaviour {
public Transform player;
private Animator anim;
public float health = 50f;
bool strangerDead = false;
Collider hitBox;
public GameObject attackCollider;
public AudioClip deathScream;
AudioSource enemy;
public AudioSource screamWhenHit;
public AudioClip [] hitScreams;
public AudioSource walkingStomp;
public AudioClip [] stomp;
public GameObject lungs;
private float lastHit = 0;
NavMeshAgent _navMeshAgent;
void Start () {
_navMeshAgent = this.GetComponent<NavMeshAgent> ();
_navMeshAgent.enabled = false;
lungs.SetActive (false);
anim = GetComponent<Animator> ();
hitBox = GetComponent<Collider> ();
attackCollider.SetActive (false);
enemy = GetComponent<AudioSource>();
}
void Update () {
_navMeshAgent.SetDestination (player.position);
lastHit += Time.deltaTime;
if (strangerDead == true)
{
attackCollider.SetActive (false);
this.enabled = false;
}
if (lastHit < 0.2)
{
return;
}
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle (direction, this.transform.forward);
if(Vector3.Distance(player.position, this.transform.position) < 15 && angle < 60 || health < 50 || lastHit == 0.2)
{
_navMeshAgent.enabled = true;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);
lungs.SetActive (true);
anim.SetBool ("isIdle", false);
if (direction.magnitude > 1.25)
{
this.transform.Translate (0, 0, 0.05f);
anim.SetBool ("isWalking", true);
anim.SetBool ("isAttacking", false);
attackCollider.SetActive (false);
}
else
{
anim.SetBool ("isAttacking", true);
anim.SetBool ("isWalking", false);
if (health > 0f)
{
}
}
}
else
{
_navMeshAgent.enabled = false;
anim.SetBool ("isIdle", true);
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", false);
attackCollider.SetActive (false);
}
}
public void TakeDamage (float amount)
{
screamWhenHit.clip = hitScreams[Random.Range(0, hitScreams.Length)];
screamWhenHit.PlayOneShot(screamWhenHit.clip);
lastHit = 0;
health -= amount;
if (health > 0f)
{
anim.Play ("Hit", 0, 1f);
anim.SetBool ("isIdle", false);
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", false);
}
if (health <= 0f)
{
Die();
}
}
void Die()
{
lungs.SetActive (false);
enemy.PlayOneShot (deathScream);
hitBox.enabled = !hitBox.enabled;
anim.SetBool ("isDead", true);
anim.SetBool ("isHit", false);
anim.SetBool ("isIdle", false);
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", false);
strangerDead = true;
}
public void ShowHitBox(){
attackCollider.SetActive (true);
}
public void HideHitBox(){
attackCollider.SetActive (false);
}
public void Stomp(){
walkingStomp.clip = stomp[Random.Range(0, stomp.Length)];
walkingStomp.PlayOneShot(walkingStomp.clip);
}
}