Search Unity

I need help with NavMeshAgent

Discussion in 'Navigation' started by julianisthebest12, Jan 9, 2021.

  1. julianisthebest12

    julianisthebest12

    Joined:
    Oct 23, 2020
    Posts:
    6
    Hi there! I'm attempting to make a simple AI that will chase the player. I followed a tutorial by Dave / GameDevelopment on youtube (
    ) and followed the instructions, but oddly when I run the game, the enemy just sits there and does nothing. Here is the code I have...
    ///
    using UnityEngine;
    using UnityEngine.AI;
    public class EnemyAiTutorial : MonoBehaviour
    {
    public NavMeshAgent agent;
    public Transform player;
    public LayerMask WhatIsGround, WhatIsPlayer;
    //Patroling
    public Vector3 walkPoint;
    bool walkPointSet;
    public float walkPointRange;
    //States
    public float sightRange, attackRange;
    public bool playerInSightRange, playerInAttackRange;
    private void Awake()
    {
    player = GameObject.Find("Player").transform;
    agent = GetComponent<NavMeshAgent>();
    }
    private void Update()
    {
    //Check for sight and attack range
    playerInSightRange = Physics.CheckSphere(transform.position, sightRange, WhatIsPlayer);
    playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, WhatIsPlayer);
    if (!playerInSightRange && !playerInAttackRange) Patroling();
    if (playerInSightRange && playerInAttackRange) ChasePlayer();

    }
    private void Patroling()
    {
    if (!walkPointSet) SearchWalkPoint();
    if (walkPointSet)
    agent.SetDestination(walkPoint);
    Vector3 distanceToWalkPoint = transform.position - walkPoint;
    //Walkpoint reached
    if (distanceToWalkPoint.magnitude < 1f)
    walkPointSet = false;
    }
    private void SearchWalkPoint()
    {
    //Calculate random point in range
    float randomZ = Random.Range(-walkPointRange, walkPointRange);
    float randomX = Random.Range(-walkPointRange, walkPointRange);
    walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
    if (Physics.Raycast(walkPoint, -transform.up, 2f, WhatIsGround))
    walkPointSet = true;
    }
    private void ChasePlayer()
    {
    agent.SetDestination(player.position);
    }
    }
    ///
    If anyone can help it would be greatly appreciated, have a great day.
     
  2. MahaloAloha

    MahaloAloha

    Joined:
    Jan 9, 2021
    Posts:
    31
    According to your syntax:

    If Player is NOT in attack range AND the Player is not in SIGHT, then the AGENTS will PATROL.
    If the Player IS in attack range AND the Player is in SIGHT, then the AGENTS will CHASE.

    I could be tired - but if your Player is NOT in attack range AND the Player IS in SIGHT, then there is no output variable. So if your NPCs are staring at the player but he is not in range, it returns a null value to AGENT output.

    You need PATROL to be called on Start and you need to have a function for what the AGENTS do when the Player is both NOT in attack range AND the player is in SIGHT.

    That's my best guess.
     
  3. MahaloAloha

    MahaloAloha

    Joined:
    Jan 9, 2021
    Posts:
    31
    Code (CSharp):
    1. if (!playerInSightRange && !playerInAttackRange) Patroling();
    2. if (playerInSightRange && playerInAttackRange) ChasePlayer();
    3.  
    4. /* There is no function for the Agents to act independently when Player is in SightRange but not in AttackRange.
    5. Come to think of it, wouldn't ChasePlayer be for (playerInSightRange && !playerInAttackRange) ? */
     
  4. julianisthebest12

    julianisthebest12

    Joined:
    Oct 23, 2020
    Posts:
    6
    Thank you so much! I was able to get it working. I just had one question, is there anyway to cause the AI to wait a second or two idle once the player is out of chase range? I figured I would have to use an IEnumerator but don't know how that would look in my code. Here's the code though
    ///
    using UnityEngine;
    using UnityEngine.AI;
    public class EnemyAiTutorial : MonoBehaviour
    {
    public NavMeshAgent agent;
    public Transform player;
    public LayerMask WhatIsGround, WhatIsPlayer;
    //Patroling
    public Vector3 walkPoint;
    bool walkPointSet;
    public float walkPointRange;
    //States
    public float sightRange;
    public bool playerInSightRange;

    private void Awake()
    {
    player = GameObject.Find("Player").transform;
    agent = GetComponent<NavMeshAgent>();
    }
    private void Update()
    {
    //Check for sight and attack range
    playerInSightRange = Physics.CheckSphere(transform.position, sightRange, WhatIsPlayer);

    if (!playerInSightRange) Patroling();
    if (playerInSightRange) ChasePlayer();

    }
    private void Start()
    {
    Patroling();

    }
    private void Patroling()
    {
    if (!walkPointSet) SearchWalkPoint();
    if (walkPointSet)
    agent.SetDestination(walkPoint);
    Vector3 distanceToWalkPoint = transform.position - walkPoint;
    //Walkpoint reached
    if (distanceToWalkPoint.magnitude < 1f)
    walkPointSet = false;
    }
    private void SearchWalkPoint()
    {
    //Calculate random point in range
    float randomZ = Random.Range(-walkPointRange, walkPointRange);
    float randomX = Random.Range(-walkPointRange, walkPointRange);
    walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
    if (Physics.Raycast(walkPoint, -transform.up, 2f, WhatIsGround))
    walkPointSet = true;
    }
    private void ChasePlayer()
    {
    agent.SetDestination(player.position);
    transform.LookAt(player);
    agent.speed = 10;
    }
    }

    ///
     
    MahaloAloha likes this.
  5. MahaloAloha

    MahaloAloha

    Joined:
    Jan 9, 2021
    Posts:
    31
    How about this, mate:

    I'll answer your question if you edit your post to be in Code Format so that my eyes don't glaze over trying to read a bunch of code that's word-wrapped.

    There's an option on the top of each posting box called "Code" with <> on it. Edit your post, give me a few hours, and I'll see what I can do.

    Also lemme drop you a PM for collab reasons and so I know when you've edited your post to the correct format.
     
  6. NixyNick

    NixyNick

    Joined:
    Oct 11, 2020
    Posts:
    12
    just use the ultimate ai system on the asset store