Help player movement for android

I’m making a small game for myself and I’m struggling to find a solution to a problem:
The code used for swiping and move the player is this but is really inaccurate. Since I’m making a 3D isometric game with movement based on screen swipe (Mobile) this works but sometimes I get the wrong input

if (Input.GetMouseButtonDown(0))
        {
            startPos = ClickedPoint();
        }
        if (Input.GetMouseButtonUp(0))
        {
            endPos = ClickedPoint();
            Vector3 distance = endPos - startPos;
            if (distance.z > 0 && Mathf.Abs(distance.z) > Mathf.Abs(distance.x))
            {
                Debug.Log("Top");
                pos = Vector3.forward;
                MovePlayers();
                return;
            }
            else if (distance.z < 0 && Mathf.Abs(distance.z) > Mathf.Abs(distance.x))
            {
                Debug.Log("Bot");
                pos = Vector3.back;
                MovePlayers();
                return;

            }
            else if (distance.x < 0 && Mathf.Abs(distance.z) < Mathf.Abs(distance.x))
            {
                Debug.Log("Left");
                pos = Vector3.left;
                MovePlayers();
                return;
            }
            else if (distance.x > 0 && Mathf.Abs(distance.z) < Mathf.Abs(distance.x))
            {
                Debug.Log("Right");
                pos = Vector3.right;
                MovePlayers();
                return;
            }
        }

This is what I would like to achieve (It can be made on the whole screen, I made every movement in a side of the base to make it easier to see)

The code above seems to be designed to give you north, south, east and west.

To me it looks like your use case wants diagonals more.

You can probably just modify the comparison logic (starting at line 9) to something like:

if (distance.x > 0 && distance.z > 0)
{
/// is up and to the right
}
if (distance.x < 0 && distance.z < 0)
{
/// is down and to the left
}
if (distance.x < 0 && distance.z > 0)
{
/// is up and to the left
}
if (distance.x > 0 && distance.z < 0)
{
/// is down and to the right
}

Depending on your coordinate system I might have that backwards what is what, but you can easily test with Debug.Log() and lots of finger swipes.

2 Likes

It works! It took me a second to understand it detects movements on a + basis and not on a X basis, so instead of moving from top left to bottom right (also following the movement the cube actually does) you move down and the cube moves down. I wasn’t expecting a different way of move the cubes not I’m saying it’s not okay, I have to remember that. THANKS

1 Like