Search Unity

How to check if touch I made is over a specific gameobject?

Discussion in 'Scripting' started by zyonneo, Jul 29, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    I was looking to draw a line on the screen.I only want to draw on the specific area of the screen .So how can I set if I could draw only on specific area. Drawing area(which is also UI) which is at the centre. That is when Drawing the lines with touch I want only to draw on the centre area avoiding the two sides.



    Code (CSharp):
    1.  if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    2. {
    3.  
    4. // if touch is only on the central part then draw.
    5.  
    6.   }
    7.  
     
    Last edited: Jul 29, 2019
  2. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    I would instead add a script to the central drawing area game object which implements the IPointClickHandler interface - this would intercept clicks/taps made on that object. You can then pass that information to a more general input controller class to decide what to do with it.

    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2.  
    3. public class DrawingArea : Monobehavior, IPointerClickHandler
    4. {
    5.     public void OnPointerClick(PointerEventData pointerEventData)
    6.     {
    7.         Debug.Log("Clicked on the drawing area!");
    8.     }
    9. }