- Home /
Mouse drag horizontally and vertically is not working..?
I want to do some stuff with game object when I click and drag my mouse horizontally(left and right) or vertically(up and down).I have written some code and it doesnt work fine.could any one please suggest some code for me.thanks in advance..!
this is my java script code..
pragma strict
ar deltaposition:Vector2; var afterdeltaposition:Vector2; var rightSwipe:boolean=false; var upSwipe:boolean=false;
//This is the speed of the Player Object. var MoveSpeed: float = 14; var mindist:float=10; //This is so we can move the position of the Collider Mesh of the Player Object around. var myCollider: BoxCollider; function Start () {
}
function Update () {
if(Input.GetMouseButtonDown(0))
{
deltaposition=Input.mousePosition;
}
if(Input.GetMouseButtonUp(0))
{
afterdeltaposition=Input.mousePosition;
}
var ydef =Mathf.Ceil(afterdeltaposition.y-deltaposition.y);
var xdef=Mathf.Ceil(afterdeltaposition.x-deltaposition.x);
var abs_ydef=Mathf.Abs(ydef);
var abs_xdef=Mathf.Abs(xdef);
if(abs_ydef>abs_xdef)
{
if((ydef>0) && (ydef>200))
{Debug.Log("swiped up");
//animation.Play("cubemove");
Debug.Log(ydef);
//Debug.Log(xdef);
ydef=0;
xdef=0;
abs_xdef=0;
abs_ydef=0;
afterdeltaposition.x=0;
afterdeltaposition.y=0;
deltaposition.x=0;
deltaposition.y=0;
}
else if((ydef<0) && (ydef<-200) )
{
Debug.Log("swiped down");
//animation.Play("cubemove");
Debug.Log(ydef);
ydef=0;
xdef=0;
abs_xdef=0;
abs_ydef=0;
afterdeltaposition.x=0;
afterdeltaposition.y=0;
deltaposition.x=0;
deltaposition.y=0;
}
}
if(abs_ydef<abs_xdef)
{
if((xdef>0) && (xdef>200))
{Debug.Log("swiped right");
//animation.Play("cubemove");
Debug.Log(xdef);
xdef=0;
ydef=0;
abs_xdef=0;
abs_ydef=0;
afterdeltaposition.x=0;
afterdeltaposition.y=0;
deltaposition.x=0;
deltaposition.y=0;
}
else if((xdef<0) && (xdef<-200))
{
Debug.Log("swiped left");
//animation.Play("cubemove");
Debug.Log(xdef);
xdef=0;
ydef=0;
abs_xdef=0;
abs_ydef=0;
afterdeltaposition.x=0;
afterdeltaposition.y=0;
deltaposition.x=0;
deltaposition.y=0;
}
}
}
Since you are not looking for drag/drop, I deleted my answer. This will mean that the question will again be marked as having no answers and be more likely to get an answer. You might want to Google "unity3d swipe detection." There are a number of posts on the issue.
Your answer
Follow this Question
Related Questions
Why are InputFields not getting activated? 1 Answer
how to detect horizantal and vertical drag on screen by mouse.? 1 Answer
How do you fill an EventTrigger from a script? 0 Answers
Input system get Vector2 mouse position by click 2 Answers
Detect a specific Key Press Event Without Keyboard Input 2 Answers