Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to check on which layer the mouse is pressed and combine it with the movement of a click

Discussion in 'Scripting' started by trmhtk2_unity, Jan 21, 2021.

  1. trmhtk2_unity

    trmhtk2_unity

    Joined:
    Mar 29, 2020
    Posts:
    35
    I have a click to move option I learned from this guide:

    The tutorial is excellent, but I do not know how to combine a test of which layer the past is clicked on, because I do not want for example, that when the mouse is clicked on the UI the character will not move foreign?
     
  2. trmhtk2_unity

    trmhtk2_unity

    Joined:
    Mar 29, 2020
    Posts:
    35
    please help me!
     
  3. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems; // Add this line
    5.  
    6. public class ClickToMove : MonoBehaviour
    7. {
    8.     ...
    9.  
    10.     void Update()
    11.     {
    12.         if (Input.GetMouseButton(0))
    13.         {
    14.             // Check if mouse button was clicked over a UI element
    15.             if (EventSystem.current.IsPointerOverGameObject())
    16.             {
    17.                 Debug.Log("Clicked on the UI");
    18.                 return;
    19.             }
    20.  
    21.             SetTargetPosition();
    22.         }
    23.  
    24.         if (isMoving)
    25.         {
    26.             Move();
    27.         }
    28.     }
    29.  
    30.     ...
    31. }
     
  4. trmhtk2_unity

    trmhtk2_unity

    Joined:
    Mar 29, 2020
    Posts:
    35
    Thanks a lot for the help, but I want to check by layer and not by GameObject there is a way?