- Home /
Help With Multitouch in 2D Windows Store apps
I have run into a problem. In my 2D scene, I have a player gameobject that rotates towards the user's finger. My code for this works fine, though I am having trouble implementing a UI consisting of two buttons; one to thrust and one to fire.
With my current code, if a user taps and holds on empty space and then, without releasing the first tap, taps the thrust button, the game works as expected. The player rotates towards the first tap and thrusts. Unfortunately, if the user taps the thrust button before tapping empty space, the player ignores the second tap. It simply rotates and thrusts towards the button.
Here is the code in the OnGUI function of a button script that detects if a user taps the button (C#):
if (isThrust == true) // Bool isThrust determines wheather the button is a thrust or shoot button
GUI.DrawTexture (new Rect(5f, Screen.height / 2f, 95f, 95f), displayImage); // the texture is then drawn in the appropriate place onscreen
else
GUI.DrawTexture (new Rect(5f, Screen.height / 2f - 100f, 95f, 95f), displayImage);
foreach (Touch touch in Input.touches) { // cycles through touches and looks for one that is over the button
if (isThrust == true)
{
if (new Rect(5f, Screen.height / 2f - 100f, 95f, 95f).Contains(GUIUtility.ScreenToGUIPoint(touch.position)))
{
GUIresult = true; // sets variable for use in determining weather to thrust/shoot
}
else
{
GUIresult = false;
}
}
else
{
if (new Rect(5f, Screen.height / 2f, 95f, 95f).Contains(GUIUtility.ScreenToGUIPoint(touch.position)))
{
GUIresult = true;
}
else
{
GUIresult = false;
}
}
}
Here is the relevant code in a script attached to the player that rotates it towards any tap not inside of a button (in FixedUpdate, C#):
foreach (Touch touch in Input.touches) { // cycles through touches
if (new Rect(5f, Screen.height / 2f - 100f, 95f, 95f).Contains(GUIUtility.ScreenToGUIPoint(touch.position)) == false && new Rect(5f, Screen.height / 2f, 95f, 95f).Contains(GUIUtility.ScreenToGUIPoint(touch.position)) == false) // makes sure that the touch is not inside one of the GUI buttons
{
dir = mainCamera.camera.ScreenToWorldPoint(touch.position) - transform.position; // sets a variable for use in calculating the rotation towards finger
break; // exits the loop to avoid cycling through extra touches
}
}
var angle = Mathf.Rad2Deg * Mathf.Atan2(dir.y, dir.x); // code to lerp the player towards finger
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.AngleAxis(angle - 90f, Vector3.forward), Time.deltaTime * rotateSpeed * clickRotateScale);
Honestly, I have probably been staring at this code for too long! Any help is greatly appreciated!