Search Unity

Equivalent of OnMouseDrag() in new input system

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

  1. yusefkerr

    yusefkerr

    Joined:
    Apr 5, 2011
    Posts:
    179
    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_Rimag

    Unity_Rimag

    Joined:
    Feb 11, 2021
    Posts:
    3
  3. jgol

    jgol

    Joined:
    Aug 16, 2020
    Posts:
    6
    Last edited: Jun 14, 2021
  4. klickaffen-studio

    klickaffen-studio

    Joined:
    Feb 18, 2017
    Posts:
    2
    works like a charm...
     
    Stevens-R-Miller likes this.
  5. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676
    Glad you folks found my approach was useful. I didn't even know I had been mentioned here until a British filmmaker (hi, Max!) who also needs this found me via this discussion. Nice to know my little tutorial is finding an audience.