Search Unity

Mouse Over GUI issue

Discussion in 'Scripting' started by Paykoman, Sep 1, 2015.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys. Im running in a little issue, im working in a click to move game and i create and empty gameobject called MouseOverUI with a script that detect if mouse is over UI so when i click over UI the player dont move, then i add the respective functions in my player script the problem is even when inventory is hided the script detect has im mouse over ui and i cant move my player to that specific position... Can anyone help me here plz?

    This are the player script and mouseovergui scripts.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(NavMeshAgent))]        //added
    5. public class ClickToMove : MonoBehaviour
    6. {
    7.     private Vector3 targetPosition;                //added
    8.  
    9.     const int LEFT_MOUSE_BUTTON = 0;            //added
    10.  
    11.     NavMeshAgent navAgent;
    12.  
    13.     public Inventory inventory;
    14.  
    15.     private Inventory bank;
    16.  
    17.     // Use this for initialization
    18.     void Awake ()
    19.     {
    20.         navAgent = GetComponent<NavMeshAgent>();
    21.     }
    22.  
    23.     void Start()
    24.     {
    25.         targetPosition = transform.position;
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update ()
    30.     {
    31.         if (Input.GetMouseButton (LEFT_MOUSE_BUTTON))
    32.             SetTargetPosition();
    33.  
    34.         MovePlayer();
    35.  
    36.         if (Input.GetKeyDown (KeyCode.I))
    37.         {
    38.             inventory.Open();
    39.         }
    40.         if(Input.GetKeyDown (KeyCode.E))
    41.         {
    42.             if(bank != null)
    43.             {
    44.                 bank.Open();
    45.             }
    46.         }
    47.     }
    48.  
    49.     void SetTargetPosition()
    50.     {
    51.         Plane plane = new Plane(Vector3.up, transform.position);
    52.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    53.         float point = 0;
    54.  
    55.         if (plane.Raycast (ray, out point))
    56.             targetPosition = ray.GetPoint (point);
    57.     }
    58.  
    59.     private void MovePlayer()
    60.     {
    61.         if (GameObject.Find ("MouseOverUI").GetComponent<MouseOverUI> ().IsmouseOverGUI)
    62.         {
    63.            
    64.         }
    65.         else
    66.         {
    67.             /*RaycastHit hit;
    68.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    69.            
    70.             if (Input.GetMouseButtonUp (0)) {
    71.                 if (Physics.Raycast (ray, out hit, 1000)) {
    72.                     navAgent.SetDestination (hit.point);
    73.                 }
    74.             }*/
    75.             navAgent.SetDestination(targetPosition);
    76.         }
    77.     }
    78.  
    79.     private void OnTriggerEnter(Collider other)
    80.     {
    81.         if (other.tag == "Item")
    82.         {
    83.             inventory.AddItem(other.GetComponent<ItemScript>());
    84.             Destroy (other.gameObject);
    85.         }
    86.  
    87.         if (other.tag == "Bank")
    88.         {
    89.             bank = other.GetComponent<BankScript>().bankInventory;
    90.         }
    91.     }
    92.  
    93.     private void OnTriggerExit(Collider other)
    94.     {
    95.         if (other.gameObject.tag == "Bank")
    96.         {
    97.             if(bank.IsOpen)
    98.             {
    99.                 bank.Open();
    100.             }
    101.             bank = null;
    102.         }
    103.     }
    104.  
    105.     private void OnColliderEnter(Collision collision)
    106.     {
    107.         if (collision.gameObject.tag == "Item")
    108.         {
    109.             inventory.AddItem(collision.gameObject.GetComponent<ItemScript>());
    110.  
    111.             Destroy(collision.gameObject);
    112.         }
    113.     }
    114. }
    115.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class MouseOverUI : MonoBehaviour
    6. {
    7.     public bool IsmouseOverGUI;
    8.  
    9.     // Update is called once per frame
    10.     void FixedUpdate ()
    11.     {
    12.         EventSystem es = EventSystem.current;
    13.  
    14.         if (es.IsPointerOverGameObject ()) {
    15.             IsmouseOverGUI = true;
    16.         }
    17.         else
    18.         {
    19.             IsmouseOverGUI = false;
    20.         }
    21.     }
    22. }
    23.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    OnColliderEnter

    this isn't a unity function... (see how it doesn't link to the api)


    also you probably don't want to check the UI state in FixedUpdate

    FixedUpdate is for physics and is not checked every frame
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    The OnColliderEnter is removed... I forgot that.

    About UI check even if i change it to Update it will check everyframe anyway right? So wt can i do here?
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That seems convoluted. Why not move all of your input over to the event system with a physics raycaster?