Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnMouseDown is not working in TouchScreen do I miss something?

Discussion in 'Scripting' started by mfatihbarut, Jan 15, 2021.

  1. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,059
    Hi all,
    OnMouseDown is not working in TouchScreen do I miss something?
    I know I can use
    void Update()
    {
    if (Input.touchCount > 0)
    if (Input.GetTouch(0).phase == TouchPhase.Began)
    *
    *
    *
    But as a crossplatform game I want to use onmousedown istead.
    Any solution?
     
  2. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,663
  3. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    You can use GetMouseButton and similarly named methods for simple mouse and touch support.
     
  4. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,059
    thanks but I got new information.
    It says if I use mousedown on my mobile game then Unity imports mouse related scripts and what should I do to save my performance?

    When building a game for a mobile device Unity examines all scripts and DLLs in the project for ANY mouse events and sets usesOnMouseEvents variable to true if it finds at least one. This variable is then used in player loop on device to start or skip mouse handling routines. This means that if you don't have any OnMouse* functions in your code these routines will be skipped not wasting CPU cycles.

    The thing is that depending on your setup these routines may be costly. When usesOnMouseEvents is true Unity does the following:

    1. Calls a method from C++ to C# which...

    2. For all cameras in your project checks if mouse cursor is within their rects;

    3. Uses camera.GetComponent<GUILayer>() in case there are IMGUI controls on screen and queries it if so;

    4. Does a 3D raycast;

    5. Does a 2D raycast;

    6. Performs a dance to figure out what OnMouse* messages to send;

    7. Sends messages using SendMessage (which is not fast);
    Do you want this code running in your game every frame just because you find using OnMouseDown easier than working with touches and raycasting manually? Your choice.
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,965
    Is it affecting the performance of the game?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    My personal suggestion is that all touch, mouse and other pointer input be handled through the EventSystem. OnMouseDown is difficult to control and is really only around for legacy code. Manual raycasting and input polling takes too much boilerplate.

    The EventSystem is a nice compromise. Out of the box it handles everything well. But it’s also flexible enough to be extended later if you need it.
     
    Voronoi and Ryiah like this.
  7. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,059
    thanks
     
  8. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    584
    I ran into this same issue and agree with @Kiwasi to use the EventSystem. I have not used interfaces before, but super easy once you've got one working. Just be sure to have a Physics Raycaster on your Camera, and an Event System in the scene as described in the documentation.

    Once you get the basic setup working, the other events are easy to understand, drag, up, down, etc. Here's an example for a click on a game object with a sphere collider that makes it 'bounce' away from the location of the click on the sphere.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class BounceOnPress : MonoBehaviour, IPointerDownHandler
    5. {
    6.  
    7.     public float speed = 10f;
    8.     public float bounce = 1f;
    9.     Vector3 startPosition;
    10.     bool isMoving = false;
    11.  
    12.     void OnEnable()
    13.     {
    14.         startPosition = transform.position;
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if (Vector3.Distance(transform.position, startPosition) > .001f)
    20.         {
    21.             Vector3 position = Vector3.Lerp(transform.position, startPosition, Time.deltaTime * speed);
    22.             transform.position = position;
    23.         }
    24.         else
    25.         {
    26.             isMoving = false;
    27.         }
    28.     }
    29.  
    30.     public void OnPointerDown(PointerEventData pointerEventData)
    31.     {
    32.         //Repeated presses move, but should not alter the return position.
    33.         if (!isMoving)
    34.             startPosition = transform.position;
    35.  
    36.         isMoving = true;
    37.  
    38.         transform.Translate(-pointerEventData.pointerCurrentRaycast.worldNormal * bounce, Space.World);
    39.     }
    40. }
     
    Kiwasi likes this.