Hi there,
I’ve got some side-scrolling shooter game for Windows Phone 8. I’m using the Input class to get touch’s for the player’s input.
The controls for the game work like so, the player holds their finger down on the screen (anywhere) and moves it to control the player sprite, like so:
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
xDirection = Input.GetTouch(0).deltaPosition.x;
yDirection = Input.GetTouch(0).deltaPosition.y;
}
}
So as you can see first touch always dictates the movement of the player.
Second comes the shooting, the player taps the screen(anywhere) to shoot, like so:
if (Input.touchCount > 1)
{
if (Input.touches[Input.touches.Length - 1].phase == TouchPhase.Began)
{
Shoot();
}
}
else if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
Shoot();
}
}
Lastly I have an enemy in the game that the player must touch to destroy, the code is like so:
if (Input.touchCount > 0)
{
Vector3 touchPosition = Camera.main.
ScreenToWorldPoint(Input.GetTouch(Input.touchCount -1).position);
if (collider2D == Physics2D.OverlapPoint(touchPosition))
{
//One-Touch kills asteroid
TakeDamage(this.Health);
}
}
Now these controls all work, to a degree but it just feels ‘buggy’ i.e. when moving the player inherently more than one touch is likely being picked up by the Input class, so sometime when you shoot there is a delay, whereas when the player isn’t being moved the shots are being fired at the rate you’re tapping the screen.
This problem also persists with the touching of the enemy mentioned in the second code snippet, where some touches aren’t registered - the enemy doesn’t die, this is only on occasion.
I’m new to Unity and it’s touch class. My best guess at the minute is it all has to do with the fact that 5? touches is the maximum amount that will be picked up, so say if the player has… fat fingers lol more than one touch is being picked up when their finger is being held on the screen moving the player.
If anyone can shed any light on a potential cause of this or see a ‘code smell’ that’d be brilliant and it’d be greatly appreciated!
Thanks in advance