Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Equivalent of OnMouseDrag() in new input system

Discussion in 'Scripting' started by yusefkerr, Nov 7, 2020.

  1. yusefkerr

    yusefkerr

    Joined:
    Apr 5, 2011
    Posts:
    162
    Hello, I'm converting a recent project to use the new input system. The project is a tarot-deck, where each playing card is an object with a collider and rigidbody. I have some functionality working great already, for example, if I want to flip a card over, I use the right mouse button like this:

    Code (CSharp):
    1. public void OnPointerDown(PointerEventData eventData)
    2.     {
    3.         if (eventData.button == PointerEventData.InputButton.Right)
    4.         {
    5.             rb.AddForce((new Vector3(0.08f, 1, 0) * flipForce), ForceMode.Impulse);
    6.             rb.AddRelativeTorque(0, 0, flipTorque, ForceMode.Impulse);
    7.         }
    8. }
    9.  
    However, I tried to add a "drag-and-drop" behaviour to the OnPointerDown() function, and I've got stuck. The documentation for the IPointerDownHandler says that it "Detects ongoing mouse clicks until release of the mouse button", so I thought I could just take my old code from an OnMouseDrag() function and paste it into OnPointerDown(). However, this only seems to fire on the first frame that I press the left mouse button. Here's my entire OnPointerDown() function, with the old code from OnMouseDrag():

    Code (CSharp):
    1. public void OnPointerDown(PointerEventData eventData)
    2.     {
    3.         if (eventData.button == PointerEventData.InputButton.Right)
    4.         {
    5.             rb.AddForce((new Vector3(0.08f, 1, 0) * flipForce), ForceMode.Impulse);
    6.             rb.AddRelativeTorque(0, 0, flipTorque, ForceMode.Impulse);
    7.         }
    8.  
    9.         if (eventData.button == PointerEventData.InputButton.Left) //What follows is the contents of the old "void OnMouseDrag()"
    10.         {
    11.             Plane plane = new Plane(Vector3.up, new Vector3(0, 0.33f, 0)); //create a new plane somewhere above the table
    12.             Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()); //create a ray from the mouse screen position to the table
    13.             float distance;
    14.  
    15.             if (!GameManager.IsPointerOverUIObject())
    16.             {
    17.                 //Plane.Raycast returns the distance along the ray, where it intersects with the plane
    18.                 if (plane.Raycast(ray, out distance))
    19.                 {
    20.                     transform.position = ray.GetPoint(distance); //sets the objects position to the point along the ray specified by "distance"
    21.  
    22.                     if (FacingUp() == true) //also rotates the cardto a standard rotation ???maybe add a random range???
    23.                     {
    24.                         transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
    25.                     }
    26.                     else
    27.                     {
    28.                         transform.rotation = Quaternion.Euler(new Vector3(0, 0, 180));
    29.                     }
    30.  
    31.                     //freeze the constraints of the object while moving
    32.                     rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
    33.                 }
    34.             }
    35.         }
    36.     }
    What approach should I take instead? It now seems obvious that I need something different for my single-fire card flip (with the right mouse button), and my every-frame card drag (with the left mouse button), but I'm not sure how the new input system is supposed to handle the latter.
     
    Last edited: Nov 7, 2020
  2. unity_XuLuBf0VtOnB-A

    unity_XuLuBf0VtOnB-A

    Joined:
    Feb 11, 2021
    Posts:
    1
unityunity