Search Unity

ZombieAI using a NavmeshAgent

Discussion in 'Navigation' started by YouBungy, May 20, 2018.

  1. YouBungy

    YouBungy

    Joined:
    Jun 19, 2017
    Posts:
    32
    Hey unity coders! I am creating this forum post because I am trying to make a following AI like a zombie AI, but I'm new to doing ai this way. I created a script by watching a few tutorials but I cant seem to get the script working. here's the script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityEngine.Animations;
    6.  
    7. public class ZombieAI : MonoBehaviour {
    8.  
    9.     public float lookradius = 10f;
    10.  
    11.     Transform target;
    12.     NavMeshAgent agent;
    13.  
    14.     //public Animator animator;
    15.  
    16.     //public Animation chase;
    17.  
    18.    // public Animation idle;
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         target = PlayerM.instance.player.transform;
    23.  
    24.         agent = GetComponent<NavMeshAgent>();
    25.  
    26.         //animator = GetComponent<Animator>();
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update () {
    31.         float distance = Vector3.Distance(target.position, transform.position);
    32.  
    33.         if (distance <= lookradius)
    34.         {
    35.             agent.SetDestination(target.position);
    36.  
    37.             //chase.enabled = true;
    38.  
    39.             if (distance <= agent.stoppingDistance)
    40.             {
    41.                 FaceTarget();
    42.          
    43.             }
    44.         }
    45.         if (distance > lookradius)
    46.         {
    47.             //chase.enabled = false;
    48.  
    49.             //idle.enabled = true;
    50.         }
    51.     }
    52.  
    53.     void FaceTarget()
    54.     {
    55.         Vector3 direction = (target.position - transform.position).normalized;
    56.         Quaternion LookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    57.         transform.rotation = Quaternion.Slerp(transform.rotation, LookRotation, Time.deltaTime * 5);
    58.     }
    59.     private void OnDrawGizmosSelected ()
    60.     {
    61.         Gizmos.color = Color.red;
    62.         Gizmos.DrawWireSphere(transform.position, lookradius);
    63.      
    64.     }
    65. }
    66.  
    I have a playermanager so its not that it wont target the player but it wont move towards it. (I am using 2018.1) The problem is the objects goes farther away from me instead of closer. if anyone could help me that would be great! thanks for reading.
     
    Last edited: May 20, 2018
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I think that the problem is that the navmeshagents use their rotation in order to know where to move and you are manipulating that rotation yourself.

    The way to get around this is to have the body of your character as a child transform of the navmeshagent. This way the navmeshagent can turn however it wants, but you can face your character a different direction.
     
  3. YouBungy

    YouBungy

    Joined:
    Jun 19, 2017
    Posts:
    32
    Good idea. I will try that, I probobly should have thought of that before
     
  4. YouBungy

    YouBungy

    Joined:
    Jun 19, 2017
    Posts:
    32
    It doesn’t seem to work