Search Unity

Such A Drag

Discussion in 'Scripting' started by VICTOM, Dec 18, 2007.

  1. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    I'm making a tutorial for my game and need to test for a left mouse button drag I'm using....

    Code (csharp):
    1.  
    2. if (Input.GetMouseButton(0)  EventType.mouseDrag) { blah }
    But blah runs on MouseButtonDown - with no mouse drag
    what gives?


    Cheers,
     
  2. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    I'm guessing drag just checks if the button is held down, not whether you are actually dragging?

    Could you check distance manually by storing the position on click, and say
    dragDistance = currentPosition - clickPosition.
    if dragDistance > x
    doBlah()

    That might also allow you to make sure people drag for a while before it completes.
     
  3. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    thanks, that is what it looks like. If this is so... then there is no need for an event that does the same thing as mouse down. I figured that at least some mouse delta would be needed to trigger the event but no. All that is needed is mouse down. :roll:

    Cheers,
     
  4. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    I mean, even if it were based on movement, one could make an argument that your mouse is always moving...

    Otherwise you'd have to define some arbitrary minimum delta, which would also seem sort of pointless.

    Then, if it stopped 'moving' in the middle of a drag, would that stop the drag function? I think that would be even worse.

    Although, I see your point with Event.
     
  5. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Is this actual code? the second check should be Event.current.type == EventType.MouseDrag
     
  6. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    NullReferenceException: Object reference not set to an instance of an object @ --> if ( Event.current.type == EventType.mouseDrag ){ blah(); }


    BTW - MouseDrag didn't compile - but mouseDrag did


    EDIT -- Only works with GUI's - I guess. Other than OnMouseDrag (which needs a collider) there is no api for mouseDrag events -- will code it myself.

    Cheers,
     
  7. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Where is this code running? Try posting the complete script
     
  8. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    It's a simple test for ANY mouseDrag event - not over a GUI or collider.

    I'm making a tutorial to teach the game skills.

    Cheers,
     
  9. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    Code (csharp):
    1. if ( Input.GetMouseButtonDown ( 0 )  Input.GetAxis("Mouse X") != 0 ){
    2.     print ("---> " +Input.GetAxis("Mouse X"));
    3. }
    Now what's wrong with this logic?
     
  10. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Event.current is only valid inside an OnGUI function - that's why I asked :)

    In your script you are checking if the mouse has a non-zero X position, not if it has moved.
     
  11. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    Silly me --

    I needed Input.mouseButton NOT Input.mouseButtonDown
    I wanted the test to keep testing while the mouse button was down not just on the first frame down.

    Cheers,