- Home /
Checking if mouse dragged from one point to another
Im tryingto check if the mouse was left clicked and passed over, lets say, position 2,0,0 to position -3,0,0, and then does an action. Like the mouse dragging over a specified path and that triggers an even. Any help is much appreciated...been at this all night lol.
When the mouse button is pressed, that can be detected with Input.$$anonymous$$ouseButtonDown(). This can set the value of a variable (say a boolean) to flag that the mouse is down. You can also record the mouse position when the mouse was clicked. When the mouse is released, you can detect that from Input.$$anonymous$$ouseButtonUp() which can change the variable again. I think this is the core of what you want.
Answer by aldonaletto · Jun 06, 2012 at 12:22 AM
You could register the mouse position when the button is pressed and measure the distance when the mouse button is released, like this:
var startPos: Vector3;
function Update(){ if (Input.MouseButtonDown(0)){ startPos = Input.mousePosition; } if (Input.MouseButtonUp(0)){ var offset = Input.mousePosition - startPos; if (offset.magnitude > 5){ print("Mouse moved more than 5 pixels"); } } } But notice that this code is operating in screen space: the distance is measured in pixels, not in the 3D world units. If you want to analyse the movement in the 3D world, things become way more complicated: you should use raycasts to find the 3D position in the mouse down and the mouse up cases, and the results could be very hard to handle - only objects that have a collider are detected by a raycast, thus you could have a mouse down point without a mouse up position, or vice versa, or no point at all (if you clicked the sky, for instance).
kind of, but Im trying to have the mouse follow a line threw an object. Alot like if you were playing fruit ninja. Having that point of contact and tracing the slash until the mouse is button is lifted
Answer by Driseus · Jun 05, 2012 at 11:14 PM
I think this could answer to your question, if i understand you right....
javascript :
var pressed : boolean = false;
function OnMouseOver(){
if(Input.GetButtonDown("Fire1"))
pressed = !pressed ;
}
C# :
bool pressed = false;
//this script just sets pressed true if the mouse is over the object,
// this script is attached too the rest is up to you...
void OnMouseOver(){
if(Input.GetButtonDown("Fire1"))
pressed = !pressed ;
}
As far as I can tell from reading this script, what it will do is toggle the boolean value stored in the "pressed" variable when the left mouse button is pressed. $$anonymous$$ore than that ... the value of pressed will be true after the first mouse button down, false after the second and true again after the third (etc) which doesn't feel very useful.
I may have been a little too quick when I corrected his code. There was two if(), one with pressed = false the other pressed = true. I assumed it was redundant, but now that I think about it, it might have been for Up and Down. Please wait Driseus answer to that, and sorry Driseus if I screwed up your code :(