Search Unity

Clicking on static object still moves NavMesh agent?

Discussion in 'Navigation' started by woody21, Jun 29, 2020.

  1. woody21

    woody21

    Joined:
    May 5, 2020
    Posts:
    1
    Hi,

    I am trying to make a point and click game using the nav mesh to control player movement. In the game I can walk to a table with objects on it (table and objects marked as static), when I try and click on the objects or the table it seems the navmesh is still detecting a click and moving the player, I also have an inventory UI and button which block the clicks from getting to the Navmesh and I would like the same to work with the game objects.

    Thanks for any help

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class PlayerMovement : MonoBehaviour
    8. {
    9.  
    10.     NavMeshAgent agent;
    11.  
    12.  
    13.     private void Start()
    14.     {
    15.         agent = GetComponent<NavMeshAgent>();
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if(Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
    21.         {
    22.             RaycastHit hit;
    23.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    24.             if(Physics.Raycast(ray, out hit, Mathf.Infinity))
    25.             {
    26.                 agent.SetDestination(hit.point);
    27.             }
    28.         }
    29.     }
    30.  
    31.  
    32. }
    33.