I have some problem with my player movement in my game, it works great on the computer/unity editor, but not so much on my android.
The game is a, or is supposed to be, a top down 2D game, were the player moves in a snapping sort of way. Im using Lerp as you can see in the code below:
Now, how do I get this to work on my Android?
I have tried a lot of stuff, but nothing seems to be working. The player is all over the place when I touch the phone. Sometimes he walks up when i press down and so on (depending on what I’ve tried ).
Do you have any ideas on what you would have done?
Is it supposed to work like this: touch above/below/left/right of character and it moves that way 1 spot?
I would try converting the player’s position to a screen co-ordinate and then checking if your touch is above/below/left/right of that, and moving accordingly.
Yeah, that has been my plan for a while, but Im having a hard time to solve it… I think the biggest problem is that i also want to prevent the player from moving diagonaly… Hmm… How would you check if you’ve pressed above/below/left/right of the player?
Well, as for preventing diagonally – you could check if it’s more to the right than up, for example. Take the larger ‘position’ to determine your move.
As for checking where they’ve pressed, relative to your player, I think an option would be to convert the player’s position to screen space and compare that position to where you touch.
if (!playerMoving)
{
if (Application.platform == RuntimePlatform.Android)
{
if (Input.touchCount > 0)
{
//get touch
Touch myTouch = Input.touches[0];
float distanceFromCamera = 10.0f;
//Get touch position
Vector3 PressedByPlayer = new Vector3(myTouch.position.x, myTouch.position.y, distanceFromCamera);
//Get player position in ScreenPoint
Vector3 PlayerInScreenSpace = Camera.main.WorldToScreenPoint(transform.position);
input.x = PressedByPlayer.x;
input.y = PressedByPlayer.y;
//check if X is higher, if it is, then make Y = 0.
//I think there's a problem here.
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
{
input.y = 0;
}
else
{
input.x = 0;
}
//if x is not 0 and the input Screen position is lesser than the player position, then go Left.
//these booleans are used down below in my Move method.
if (PlayerInScreenSpace.x > input.x && input.x != 0)
{
playerGoLeft = true;
playerGoRight = false;
}
else if (PlayerInScreenSpace.x < input.x && input.x != 0)
{
playerGoRight = true;
playerGoLeft = false;
}
else if (PlayerInScreenSpace.y > input.y && input.y != 0)
{
playerGoDown = true;
playerGoUp = false;
}
else if (PlayerInScreenSpace.y < input.y && input.y != 0)
{
playerGoUp = true;
playerGoDown = false;
}
if (input != Vector2.zero)
{
StartCoroutine(Move(transform));
}
}
When I run this the player can mostly just go right or left. Since X is almost always bigger then Y on my screen when i touch the screen.
See Uploaded file. I want the player object to go down, instead it’s going left. Maybe I should go with ViewPort instead? Although already tried that aswell.
Ya, I think instead of input.x > input.y you want to check if the input.x is more left/right of the player, as compared to the input.y and its closeness to the player on screen position.y
that sort of thing. I didn’t really look closely at your code beyond that, since you said you think that’s mostly the problem.
If the player’s position is say 4, 4 in screen space (just for example purposes)
and you click at 3, 8
4 and 3 … diff = 1
4 and 8 … diff = 4 So ‘y’ wins here.