- Home /
A problem with 2d swipe controls.
Hey, I've been making a game and the last thing I needed to do was the swipe controls, I thought it was going to be easy because unity will have something implemented for us, but no, It is way harder than I thought so I had to follow a tutorial. what ended up happening is that my code isn't working, but I know the tutorial is good just that there is a problem in the code I wrote from the video (the video didn't have a copy of the code linked). What my code looks like now:
private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown, isDraging;
private Vector2 startTouch, swipeDelta;
private float moveSpeed = 1.5f;
private void Update()
{
tap = swipeLeft = swipeRight = swipeUp = swipeDown = isDraging = false;
#region Standalone Input
//click
if (Input.GetMouseButtonDown(0))
{
tap = true;
isDraging = true;
startTouch = Input.mousePosition;
}
//release
else if (Input.GetMouseButtonUp(0))
{
isDraging = false;
Reset();
}
#endregion
#region Moblie Inputs
if(Input.touches.Length > 0)
{
if(Input.touches[0].phase == TouchPhase.Began)
{
isDraging = true;
tap = true;
startTouch = Input.touches[0].position;
}
else if(Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
isDraging = false;
Reset();
}
}
#endregion
#region Swipe Check
swipeDelta = Vector2.zero;
if (isDraging)
{
if (Input.touches.Length > 0)
{
swipeDelta = Input.touches[0].position - startTouch;
}
else if (Input.GetMouseButton(0))
{
swipeDelta = new Vector2(Input.mousePosition.x, Input.mousePosition.y) - startTouch;
}
}
Debug.Log(swipeDelta);
if (swipeDelta.magnitude > 125)
{
//which direction are we swiping in
float x = swipeDelta.x;
float y = swipeDelta.y;
if(Mathf.Abs(x) > Mathf.Abs(y))
{
//left or right
if(x < 0)
{
swipeLeft = true;
}
else
{
swipeRight = true;
}
}
else
{
//up or down
if(y < 0)
{
swipeDown = true;
}
else
{
swipeUp = true;
}
}
Reset();
}
#endregion
the swipeDelta is always 0.0, 0.0. The startTouch is working fine and showing the position of the click. I have no idea what could be wrong here, I've checked the code a few times now, haven't found anything suspicious yet.
Answer by Llama_w_2Ls · Jul 02, 2021 at 12:09 PM
I followed that tutorial a long time ago. In my opinion, it's overly complicated. Swipe controls shouldn't have to look this daunting.
The first thing you're going to want to do, is find out the direction in which you swiped. You can do this in the Update method, using the mouse position when you first clicked, and the mouse position when you let go of the mouse as your direction. For example:
Vector3 StartPos;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartPos = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
var swipeDirection = Input.mousePosition - StartPos;
// Do something with swipeDirection...
}
}
Finally, you need to pass in the raw direction into a method, that processes the direction and outputs a swipe direction value. For example:
void GetSwipeDirection(Vector3 direction)
{
// If the swipe was unnoticable (accidental touch) don't move
if (direction.magnitude < DeadZone)
return;
var normalizedDir = direction.normalized;
// If the swipe was diagonal, return the closest straight
// direction (so North-North-East = North)
normalizedDir = Mathf.Abs(normalizedDir.x) > Mathf.Abs(normalizedDir.y)
? new Vector3(normalizedDir.x, 0)
: new Vector3(0, normalizedDir.y);
// Round values to nearest 1
var adjustedDir = new Vector2Int
(
Mathf.RoundToInt(normalizedDir.x),
Mathf.RoundToInt(normalizedDir.y)
);
// Player movement function (moves the player in that direction)
player.Move(adjustedDir);
}
Full Script
public class SwipeControls : MonoBehaviour
{
public Player player;
public float DeadZone;
Vector3 StartPos;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartPos = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
var swipeDirection = Input.mousePosition - StartPos;
GetSwipeDirection(swipeDirection);
}
}
void GetSwipeDirection(Vector3 direction)
{
// If the swipe was unnoticable (accidental touch) don't move
if (direction.magnitude < DeadZone)
return;
var normalizedDir = direction.normalized;
// If the swipe was diagonal, return the closest straight
// direction (so North-North-East = North)
normalizedDir = Mathf.Abs(normalizedDir.x) > Mathf.Abs(normalizedDir.y)
? new Vector3(normalizedDir.x, 0)
: new Vector3(0, normalizedDir.y);
// Round values to nearest 1
var adjustedDir = new Vector2Int
(
Mathf.RoundToInt(normalizedDir.x),
Mathf.RoundToInt(normalizedDir.y)
);
// Player movement function (moves the player in that direction)
player.Move(adjustedDir);
}
}
Your answer
Follow this Question
Related Questions
2d swipe control not working. 1 Answer
Creating 'redstone' like wires 1 Answer
Structuring complex 2D sprite animation 0 Answers
Level design clone of King of Thieves 0 Answers
Horizontal attraction? 0 Answers