Search Unity

Navmesh agent is NOT working. Please help!

Discussion in 'Navigation' started by SkyBreaker_, Nov 8, 2019.

  1. SkyBreaker_

    SkyBreaker_

    Joined:
    Jun 22, 2019
    Posts:
    2
    I'm currently working on a small dungeon crawler which uses a point and click system for movement. I have some scripts setup for defining special targets (for things like chests, doors and enemies) and got the player navigating to them previously. However now it seems the player REFUSES to go to my Chest target script and I cannot work out why. Any help would be greatly appreciated :)

    Here's the player controller script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using Dungeon.Base;
    6.  
    7. namespace Dungeon.Player
    8. {
    9.     [AddComponentMenu("Dungeon/Player/Player Controller")]
    10.     [RequireComponent(typeof(NavMeshAgent))]
    11.     public class PlayerController : MonoBehaviour
    12.     {
    13.         [Header ("Controller")]
    14.         public NavMeshAgent agent;
    15.         public Animator anim;
    16.  
    17.         Vector3 current;
    18.         Vector3 previous;
    19.  
    20.         Transform followTarget;
    21.  
    22.         [Header ("Camera")]
    23.         public float sensitivity = 500;
    24.         [Space]
    25.         public float min = 15;
    26.         public float max = 60;
    27.         [Space]
    28.         public bool doInput;
    29.         public static PlayerController plr;
    30.         public Transform locationMarker;
    31.         public Transform pointer;
    32.         Vector3 targetLocation;
    33.  
    34.         private void OnValidate()
    35.         {
    36.             if (agent == null)
    37.             {
    38.                 agent = GetComponent<NavMeshAgent>();
    39.             }
    40.         }
    41.  
    42.         private void Awake()
    43.         {
    44.             plr = this;
    45.  
    46.             current = transform.position;
    47.             previous = transform.position;
    48.         }
    49.  
    50.         public void Halt ()
    51.         {
    52.             followTarget = null;
    53.             agent.isStopped = true;
    54.         }
    55.  
    56.         private void Update()
    57.         {
    58.             targetLocation = agent.destination;
    59.  
    60.             current = transform.position;
    61.  
    62.             if (followTarget != null)
    63.             {
    64.                 agent.isStopped = false;
    65.                 agent.SetDestination(followTarget.position);
    66.  
    67.                 print("Navigating to " + followTarget.name);
    68.  
    69.                 locationMarker.position = followTarget.position;
    70.  
    71.                 if (Vector3.Distance (transform.position, followTarget.position) < followTarget.GetComponent<SelectionTarget>().stopDistance)
    72.                 {
    73.                     Halt();
    74.  
    75.                     if (followTarget != null)
    76.                     {
    77.                         if (followTarget.GetComponent<SelectionTarget>())
    78.                         {
    79.                             if (followTarget.GetComponent<SelectionTarget>().GetSelection() == SelectType.Enemy)
    80.                             {
    81.                                 //Attack Behaviour
    82.                             }
    83.                             else if (followTarget.GetComponent<SelectionTarget>().GetSelection() == SelectType.Item)
    84.                             {
    85.                                 //Grab Behaviour
    86.                                 anim.SetTrigger("interact");
    87.                                 followTarget.GetComponent<SelectionTarget>().OnInteract();
    88.                             }
    89.                             else if (followTarget.GetComponent<SelectionTarget>().GetSelection() == SelectType.Door)
    90.                             {
    91.                                 //Door Behaviour
    92.                                 anim.SetTrigger("interact");
    93.                                 followTarget.GetComponent<SelectionTarget>().OnInteract();
    94.                             }
    95.                             else if (followTarget.GetComponent<SelectionTarget>().GetSelection() == SelectType.Switch)
    96.                             {
    97.                                 //Switch Behaviour
    98.                                 anim.SetTrigger("interact");
    99.                                 followTarget.GetComponent<SelectionTarget>().OnInteract();
    100.                             }
    101.                             else if (followTarget.GetComponent<SelectionTarget>().GetSelection() == SelectType.Chest)
    102.                             {
    103.                                 //Chest Behaviour
    104.                                 anim.SetTrigger("interact");
    105.                                 followTarget.GetComponent<SelectionTarget>().OnInteract();
    106.                             }
    107.                         }
    108.                     }
    109.  
    110.                     followTarget = null;
    111.                 }
    112.             }
    113.  
    114.             if (doInput == true)
    115.             {
    116.                 if (pointer != null)
    117.                 {
    118.                     RaycastHit p;
    119.  
    120.                     if (Physics.Raycast(CameraController.cam.ScreenPointToRay(Input.mousePosition), out p, 100f))
    121.                     {
    122.                         pointer.position = p.point;
    123.                     }
    124.                 }
    125.  
    126.                 if (Input.GetButtonDown("Fire2"))
    127.                 {
    128.                     Halt();
    129.                 }
    130.  
    131.                 if (Input.GetButtonDown("Fire1"))
    132.                 {
    133.                     RaycastHit h;
    134.  
    135.                     if (Physics.Raycast(CameraController.cam.ScreenPointToRay(Input.mousePosition), out h, 100f))
    136.                     {
    137.                         agent.isStopped = false;
    138.  
    139.                         if (h.collider.GetComponent<SelectionTarget>())
    140.                         {
    141.                             print("SelectionTargeted");
    142.                             followTarget = h.collider.transform;
    143.                         }
    144.                         else
    145.                         {
    146.                             locationMarker.position = h.point;
    147.                             agent.SetDestination(h.point);
    148.                         }
    149.                     }
    150.                 }
    151.  
    152.                 if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
    153.                 {
    154.                     FindObjectOfType<CameraController>().ChangeOffset(1);
    155.                 }
    156.  
    157.                 if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
    158.                 {
    159.                     FindObjectOfType<CameraController>().ChangeOffset(-1);
    160.                 }
    161.  
    162.                 if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.W))
    163.                 {
    164.                     FindObjectOfType<CameraController>().SetOffset(0);
    165.                     FindObjectOfType<CameraController>().td = true;
    166.                 }
    167.  
    168.                 if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
    169.                 {
    170.                     FindObjectOfType<CameraController>().SetOffset(0);
    171.                     FindObjectOfType<CameraController>().td = false;
    172.                 }
    173.  
    174.                 CameraController.cam.fieldOfView += (-Input.GetAxisRaw("Mouse ScrollWheel") * Time.deltaTime) * sensitivity;
    175.                 CameraController.cam.fieldOfView = Mathf.Clamp(CameraController.cam.fieldOfView, min, max);
    176.             }
    177.  
    178.             if (current.magnitude - previous.magnitude != 0)
    179.             {
    180.                 anim.SetBool("move", true);
    181.                 locationMarker.gameObject.SetActive(true);
    182.             } else
    183.             {
    184.                 anim.SetBool("move", false);
    185.                 locationMarker.gameObject.SetActive(false);              
    186.             }  
    187.            
    188.             previous = transform.position;
    189.         }
    190.     }
    191. }
    192.  
     
  2. SkyBreaker_

    SkyBreaker_

    Joined:
    Jun 22, 2019
    Posts:
    2
    Nevermind I fixed it. Turns out I accidentally added a NavMeshObstacle component onto my chest object :|
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I'm glad you got that off your chest.
     
    TheOneRaven likes this.