Support Forum

Click through UI canvas problem

Hello, I’m using AILerp with Seeker and AI Destination Setter to a Target emptyobject witch supposed to have left mouse click function to move Agent in a 2D grid isometric A* Pathfinding system.
It’s working as expected but I can’t figure out how to block left click through UI canvas.
I’ve done the following without success:
Script used with left mouse click: TargetMove.cs

  1. EventSystem Checking. (EventSystem.current.IsPointerOverGameObject())
  2. Event Trigger
  3. EventSystem Interface
  4. Click Manager with raycast

Nothing of these solution work. I’m losing hope right now.

Here’s how I do it.

if (!EventSystem.current.IsPointerOverGameObject())
  { 
  }

I also watched the first video. The scene should have an “event system”.

2 Likes

Thank you for reply.
Still not working unfortunally like I said I alredy tested that method.
If it had been that simple I wouldn’t have asked for help :frowning:

This is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;
using System;

namespace Pathfinding {

	public class MovementScript : MonoBehaviour {
		/// <summary>Mask for the raycast placement</summary>
		public LayerMask mask;

		public Transform target;

		IAstarAI[] ais;

		/// <summary>Determines if the target position should be updated every frame or only on double-click</summary>
		public bool onlyOnClick;
		public bool use2D;


		Camera cam;


		public void Start () {
			//Cache the Main Camera
			cam = Camera.main;
			// Slightly inefficient way of finding all AIs, but this is just an example script, so it doesn't matter much.
			// FindObjectsOfType does not support interfaces unfortunately.
			ais = FindObjectsOfType<MonoBehaviour>().OfType<IAstarAI>().ToArray();
			useGUILayout = false;
		}

		public void OnGUI()
		{
 			if (onlyOnClick && cam != null && Mouse.current.leftButton.wasPressedThisFrame && !EventSystem.current.IsPointerOverGameObject())
			{
				UpdateTargetPosition();
			}
		}

		/// Update is called once per frame
		void Update ()
		{
			if (Mouse.current.leftButton.wasPressedThisFrame && !EventSystem.current.IsPointerOverGameObject())
			{
				if (!onlyOnClick && cam != null)
				{
					UpdateTargetPosition();
				}
			}

		}


		public void UpdateTargetPosition () {
			Vector3 newPosition = Vector3.zero;
			bool positionFound = false;

			if (use2D) {
				newPosition = cam.ScreenToWorldPoint(Mouse.current.position.ReadValue());
				newPosition.z = 0;
				positionFound = true;
			} 
			else
			{
				// Fire a ray through the scene at the mouse position and place the target where it hits
				RaycastHit hit;
				if (Physics.Raycast(cam.ScreenPointToRay(Mouse.current.position.ReadValue()), out hit, Mathf.Infinity, mask)) {
					newPosition = hit.point;
					positionFound = true;
				}
			}

			if (positionFound && newPosition != target.position)
			{
				target.position = newPosition;

				if (onlyOnClick)
				{
					for (int i = 0; i < ais.Length; i++) {
						if (ais[i] != null) ais[i].SearchPath();
					}
				}
			}
		}
	}
}

My hierarchy:

I’m using unity 2022.1

I can’t fix the code because I’m the same as you. Here the problem is not in the library, it is only responsible for the movement, and not for guidance. Try to remove all unnecessary and see how the script works.

public void OnGUI()
		{
 			if (!EventSystem.current.IsPointerOverGameObject())
			{
				print("1");
			}
		}

		/// Update is called once per frame
		void Update ()
		{
			if ( !EventSystem.current.IsPointerOverGameObject())
			{
				print("2");
			}

		}

Hover over the canvas, and then move your cursor off it. Look further at the circumstances. Add the code to “if” first.

1 Like

Good old method always work.

Thank you AHAHAH

apparently using a 2d collider on the canvas where you want to limit the click solves the problem with !EventSystem.current.IsPointerOverGameObject()

Here is the code:

		public void OnGUI()
		{
 			if (onlyOnClick && cam != null && Mouse.current.leftButton.wasPressedThisFrame)
			{
				if(EventSystem.current.IsPointerOverGameObject())
				{
					// UpdateTargetPosition();
					Debug.Log("1");
				}
				else if(!EventSystem.current.IsPointerOverGameObject())
				{
					UpdateTargetPosition();
					Debug.Log("2");
				}
			}
		}

log “1” only showed if I clicked on the agent location so I suspected it interacted with the rigidbody.
And that’s it.
Thank you for your time. :smile:

I hope the explanation will be useful for other users in the future as well!

Update:
Little solution for people that have the same problem and want to not click outside the screensize:

		public void OnGUI()
		{
        	Rect boderlimit = new Rect(0, 0, Screen.width, Screen.height);
 			if (onlyOnClick && Mouse.current.leftButton.wasPressedThisFrame && boderlimit.Contains(Mouse.current.position.ReadValue()))
			{
				if(!EventSystem.current.IsPointerOverGameObject())
				{
					UpdateTargetPosition();
				}
			}
		}

Of course change to your project input, I’m using new Unity input system (UnityEngine.InputSystem)

2 Likes