- Home /
Swipe Movement On A Grid
I'm making a game like tetris for mobile devices. It's movement should be on a grid. When i swipe right or left, the object moves 1 unit to right or left. No matter how much i drag it, it just moves 1 unit. But i want it to move more than 1 unit if i drag it longer. How can i achieve that? The game Tetris in google play store uses the method i want but i dont know how they did it. So please help. Here is the script that makes it move once:
private bool fingerDown;
public Vector2 startPos;
public int pixelDistToDetect = 41;
// Update is called once per frame
void Update()
{
if(!fingerDown && Input.GetMouseButtonDown(0))
{
startPos = Input.mousePosition;
fingerDown = true;
}
if (fingerDown)
{
if(Input.mousePosition.x >= startPos.x + pixelDistToDetect)
{
fingerDown = false;
gameObject.transform.position += new Vector3(0.82f, 0, 0);
startPos = Input.mousePosition;
return;
}
if (Input.mousePosition.x <= startPos.x - pixelDistToDetect)
{
fingerDown = false;
gameObject.transform.position -= new Vector3(0.82f, 0, 0);
startPos = Input.mousePosition;
return;
}
}
if(fingerDown && Input.GetMouseButtonUp(0))
{
fingerDown = false;
}
i made it with mouse input for testing. It will be touch input in the final.
Answer by metalted · Dec 06, 2021 at 05:50 PM
I think your problem lies with the fact that you have "fingerDown = false" on line 17 and line 26. The logic is fine, you check if the finger is down and if the finger is down you check the distance between the starting point and the current position. If the distance is greater than the set value, you move the object and set a new start point.
BUT! , this is not an indication the player has removed their finger from the screen and therefore, fingerDown should still be true to register the movement, otherwise it will only start tracking again if the player puts a finger back on the screen, because that is the only point in your code where fingerDown is set to true.
I know that lines are preventing it from going multiple times but when i remove them this happens: 1) I click and finger down becomes true. 2) I drag it beyond the threshold 3)It moves until i release the button (Far far away)
I want it to move as much as i moved my mouse. Same distance. But i cant figure it out.
I'm not really sure what the problem is. If i use this exact code without the fingerDown=false part, it works perfectly for me. I hold down the mouse, move to the right 41 pixels, the object moves 0.82 units to the right. If i then keep dragging, mouse button still down, 41 pixels to the right, the object moves again 0.82 units. This works until i release the mouse button. Is this not the effect you are going for?
Yea exactly. But in my case, it moves until i let go. Do you use it in update function? Can you give me the code you're using?
Your answer
Follow this Question
Related Questions
Continual in-game directional cursor movement help 0 Answers
Angry Birds hold, drag and catapult an object ... 2 Answers
Snap to grid after click and drag? 2 Answers
How to drag gameobject (only x y) snapped to a grid? 2 Answers
How can I calculate a list of points where a line intersects a grid? 1 Answer