Search Unity

Navmesh and inventory

Discussion in 'Navigation' started by OneProGoober, Aug 13, 2017.

  1. OneProGoober

    OneProGoober

    Joined:
    May 11, 2016
    Posts:
    50
    Hey guys, I have a click to move game and I have an inventory set up on a canvas with an image that gets rendered over the game screen. How do I go about clicking over the inventory. Currently the player will move if I click the inventory and I obviously don't want this functionality.

    I RESOLVED my issue!!!!!!!!:
    Okay, so I actually changed my movement around to just make it so right click moves my character and left click is for interaction.
     
    Last edited: Aug 13, 2017
  2. IvanDonets

    IvanDonets

    Joined:
    Feb 19, 2014
    Posts:
    117
    no need to make it different buttons. In my game I had similar problem, but I used script like this (attached to UI elements):
    Code (CSharp):
    1.  
    2. public class UIButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    3. {
    4. //... some variables etc
    5.   public void OnPointerEnter(PointerEventData eventData)
    6.     {
    7.               NavMeshPlayerScript.RaycastingUI = true;
    8.     }
    9.     public void OnPointerExit(PointerEventData eventData)
    10.     {    
    11.         NavMeshPlayerScript.RaycastingUI = false;
    12.     }
    13. }
    14.  
    So when mouse goes over UI, it switches RaycastingUI variable to true, and when mouse leaves UI it switches RaycastingUI to false... And in player's script just check this variable, and if raycasting - then don't move, don't get new destination point etc...
     
  3. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    Another way is to just have a Ui element behind everything.
    If this is clicked you can do your raycast for movement in it's on click or it's onpointerclicked.

    Then the normal Ui masking will not call this if you click another in front Ui element.
     
  4. TSabos

    TSabos

    Joined:
    Mar 8, 2015
    Posts:
    94
    https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html

    This will return if you're hovering any of the UI or not.

    If you're using a PhysicsRaycaster in your game to use the new event system this will not work though, you can use the following in Update() before your raycasts instead:
    Code (CSharp):
    1.         PointerEventData pointerData = new PointerEventData(EventSystem.current);
    2.         pointerData.position = Input.mousePosition;
    3.         List<RaycastResult> results = new List<RaycastResult>();
    4.         gr.Raycast(pointerData, results);
    5.         PointerOverUI = results.Count > 0;
    6.