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.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(NavMeshAgent))] //added
public class ClickToMove : MonoBehaviour
{
private Vector3 targetPosition; //added
const int LEFT_MOUSE_BUTTON = 0; //added
NavMeshAgent navAgent;
public Inventory inventory;
private Inventory bank;
// Use this for initialization
void Awake ()
{
navAgent = GetComponent<NavMeshAgent>();
}
void Start()
{
targetPosition = transform.position;
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButton (LEFT_MOUSE_BUTTON))
SetTargetPosition();
MovePlayer();
if (Input.GetKeyDown (KeyCode.I))
{
inventory.Open();
}
if(Input.GetKeyDown (KeyCode.E))
{
if(bank != null)
{
bank.Open();
}
}
}
void SetTargetPosition()
{
Plane plane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float point = 0;
if (plane.Raycast (ray, out point))
targetPosition = ray.GetPoint (point);
}
private void MovePlayer()
{
if (GameObject.Find ("MouseOverUI").GetComponent<MouseOverUI> ().IsmouseOverGUI)
{
}
else
{
/*RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonUp (0)) {
if (Physics.Raycast (ray, out hit, 1000)) {
navAgent.SetDestination (hit.point);
}
}*/
navAgent.SetDestination(targetPosition);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Item")
{
inventory.AddItem(other.GetComponent<ItemScript>());
Destroy (other.gameObject);
}
if (other.tag == "Bank")
{
bank = other.GetComponent<BankScript>().bankInventory;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Bank")
{
if(bank.IsOpen)
{
bank.Open();
}
bank = null;
}
}
private void OnColliderEnter(Collision collision)
{
if (collision.gameObject.tag == "Item")
{
inventory.AddItem(collision.gameObject.GetComponent<ItemScript>());
Destroy(collision.gameObject);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class MouseOverUI : MonoBehaviour
{
public bool IsmouseOverGUI;
// Update is called once per frame
void FixedUpdate ()
{
EventSystem es = EventSystem.current;
if (es.IsPointerOverGameObject ()) {
IsmouseOverGUI = true;
}
else
{
IsmouseOverGUI = false;
}
}
}