Search Unity

NavMesh is working, but the enemy still can't walk around obstacles

Discussion in 'Navigation' started by BorjaZoroza, Jan 29, 2019.

  1. BorjaZoroza

    BorjaZoroza

    Joined:
    Aug 14, 2017
    Posts:
    28
    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!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class StrangerBehaviour : MonoBehaviour {
    7.  
    8.     public Transform player;
    9.     private Animator anim;
    10.     public float health = 50f;
    11.     bool strangerDead = false;
    12.     Collider hitBox;
    13.     public GameObject attackCollider;
    14.  
    15.     public AudioClip deathScream;
    16.     AudioSource enemy;
    17.  
    18.     public AudioSource screamWhenHit;
    19.     public AudioClip [] hitScreams;
    20.  
    21.  
    22.     public AudioSource walkingStomp;
    23.     public AudioClip [] stomp;
    24.  
    25.  
    26.     public GameObject lungs;
    27.  
    28.  
    29.     private float lastHit = 0;
    30.  
    31.     NavMeshAgent _navMeshAgent;
    32.  
    33.  
    34.     void Start () {
    35.  
    36.         _navMeshAgent = this.GetComponent<NavMeshAgent> ();
    37.  
    38.         _navMeshAgent.enabled = false;
    39.  
    40.         lungs.SetActive (false);
    41.  
    42.         anim = GetComponent<Animator> ();
    43.         hitBox = GetComponent<Collider> ();
    44.         attackCollider.SetActive (false);
    45.  
    46.         enemy = GetComponent<AudioSource>();
    47.  
    48.     }
    49.        
    50.     void Update () {
    51.         _navMeshAgent.SetDestination (player.position);
    52.  
    53.         lastHit += Time.deltaTime;
    54.  
    55.         if (strangerDead == true)
    56.         {
    57.             attackCollider.SetActive (false);
    58.             this.enabled = false;
    59.         }
    60.  
    61.         if (lastHit < 0.2)
    62.         {
    63.             return;
    64.         }
    65.  
    66.         Vector3 direction = player.position - this.transform.position;
    67.         float angle = Vector3.Angle (direction, this.transform.forward);
    68.         if(Vector3.Distance(player.position, this.transform.position) < 15 && angle < 60 || health < 50 || lastHit == 0.2)
    69.         {
    70.             _navMeshAgent.enabled = true;
    71.  
    72.             direction.y = 0;
    73.  
    74.             this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);
    75.  
    76.             lungs.SetActive (true);
    77.  
    78.             anim.SetBool ("isIdle", false);
    79.             if (direction.magnitude > 1.25)
    80.             {
    81.                 this.transform.Translate (0, 0, 0.05f);
    82.                 anim.SetBool ("isWalking", true);
    83.                 anim.SetBool ("isAttacking", false);
    84.                 attackCollider.SetActive (false);
    85.             }
    86.             else
    87.             {
    88.                 anim.SetBool ("isAttacking", true);
    89.                 anim.SetBool ("isWalking", false);
    90.                 if (health > 0f)
    91.                 {
    92.                 }
    93.             }
    94.         }
    95.         else
    96.         {
    97.             _navMeshAgent.enabled = false;
    98.             anim.SetBool ("isIdle", true);
    99.             anim.SetBool ("isWalking", false);
    100.             anim.SetBool ("isAttacking", false);
    101.             attackCollider.SetActive (false);
    102.         }
    103.  
    104.     }
    105.  
    106.     public void TakeDamage (float amount)
    107.     {
    108.         screamWhenHit.clip = hitScreams[Random.Range(0, hitScreams.Length)];
    109.         screamWhenHit.PlayOneShot(screamWhenHit.clip);
    110.  
    111.         lastHit = 0;
    112.         health -= amount;
    113.         if (health > 0f)
    114.         {
    115.         anim.Play ("Hit", 0, 1f);
    116.         anim.SetBool ("isIdle", false);
    117.         anim.SetBool ("isWalking", false);
    118.         anim.SetBool ("isAttacking", false);
    119.         }
    120.  
    121.         if (health <= 0f)
    122.         {
    123.             Die();
    124.         }
    125.     }
    126.  
    127.     void Die()
    128.     {
    129.  
    130.         lungs.SetActive (false);
    131.  
    132.         enemy.PlayOneShot (deathScream);
    133.  
    134.         hitBox.enabled = !hitBox.enabled;
    135.         anim.SetBool ("isDead", true);
    136.         anim.SetBool ("isHit", false);
    137.         anim.SetBool ("isIdle", false);
    138.         anim.SetBool ("isWalking", false);
    139.         anim.SetBool ("isAttacking", false);
    140.         strangerDead = true;
    141.  
    142.     }
    143.  
    144.  
    145.     public void ShowHitBox(){
    146.  
    147.         attackCollider.SetActive (true);
    148.        
    149.     }
    150.  
    151.     public void HideHitBox(){
    152.  
    153.         attackCollider.SetActive (false);
    154.  
    155.     }
    156.  
    157.     public void Stomp(){
    158.  
    159.         walkingStomp.clip = stomp[Random.Range(0, stomp.Length)];
    160.         walkingStomp.PlayOneShot(walkingStomp.clip);
    161.  
    162.     }
    163. }
    164.  
    165.  
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Well you seem to both want to move using the NavMeshAgent as well as using transform.Translate.
    That's not how it works, if you're using the NavMesh, ALL your movement goes through that until you decide to disable it. I'm pretty certain that this translate is pushing you off of your path and into those obstacles. Same with the rotation you try to apply at first. NavMeshAgents rotate towards their direction along the path already.

    On a sidenote, please split this class up. Components are there to be small and only bother with a select few tasks.
    Movement code should not be concerned with audio playback and animation, for example.
    It will make things a lot easier for yourself since you won't have to frantically search where strange things are happening, and it'll be a ton easier to post your code around without having everyone spending time reading irrelevant code.
     
    Bakanovskiy95 likes this.
  3. BorjaZoroza

    BorjaZoroza

    Joined:
    Aug 14, 2017
    Posts:
    28
    Oh, that fixed it! Thank you! And you're right about leaving out the irrelevant parts of the code, I'm sorry.
     
    Yandalf likes this.
  4. gliealonso

    gliealonso

    Joined:
    Oct 21, 2018
    Posts:
    117

    Almost 3 years later and your message has helped me! Thanks a bunch!
     
    Akib02 likes this.