MultiTouch for control scheme of my game (C#)

Hey Guys.

I’m fairly new to Unity as well as C#, and I have game mostly developed, but I can’t figure out one thing. My game is for mobile devices (IE Galaxy S5, iPhone 5s) and I have three buttons on screen that control the movement of my character. One that makes it jump, one that when held, makes it move right, and another that when held, makes it move left. That all works fine, but there is one issue.

If the player is currently holding down either the ‘move left’ or ‘move right’ buttons, then they cannot tap on the jump button in order to jump. If they stop holding the movement buttons and THEN tap the jump button, it works fine.

I have read a few threads on Multitouch, but they all assume/associate with using a single script. Each of my buttons uses a different script in order to keep things simplified and spread out.

The question I have here is: how could I incorporate Multitouch through about 3 different scripts, and allow players to hold down one finger AND press another, but only on the buttons? For reference, here is an image of my game:

30712-capture.png

As you see, there are the three buttons. The user can move left and right while holding down their associated buttons with ease, but when holding them down, they cannot press the jump button in order to hop up. Remember, the controls are spread out across 3 separate scripts.

I think using one script would be more efficient. I ripped this script from an old project I made, so it wont fit straight into yours.

It checks to see if a Rect I’ve placed over GUI buttons contains a touch. This is just to give you an idea, its not a complete solution.

        foreach(Touch t in Input.touches)
		{
			Vector2 vec = t.position;
			vec.y = Screen.height - vec.y; // You need to invert since GUI and screen have differnet coordinate system
			
			if(testgame1.buttonLeftRect.Contains(vec))
			{
				weaponContainer.gameObject.transform.Rotate(0,0,speed);
				Debug.Log("touched");
			}
			
			if(testgame1.buttonRightRect.Contains(vec))
			{
				weaponContainer.gameObject.transform.Rotate(0,0,-speed);
			}
			
			if(!fireActive)
			{
				if(testgame1.buttonFireRect.Contains(vec))
				{
					//fireActive = true;
					
					if(t.phase == TouchPhase.Began)
					{
						kick.animation.Play("test anim");
						GameObject clone = GameObject.Instantiate(bullet,spawnPoint.transform.position,spawnPoint.transform.rotation) as GameObject;	
					}
					if(t.phase == TouchPhase.Ended )
					{
						//fireActive = false;
					}
				}
			}
		}

I hope this can be useful to you :slight_smile:


EDIT: More info.

You could try something like this…

int currentTouch;

	//----if there is no touch...
	if(Input.touches.Length <= 0)
	{
		//Do something
	}
	//if there is touch...
	else 
	{
		for(int i = 0; i < Input.touchCount; i++)//loop through touches on screen
		{
			currentTouch = i;

			//Check touch phase...
			if(Input.GetTouch(currentTouch).phase == TouchPhase.Stationary)
			{
				//Get current touch position...
				var ct = Input.GetTouch(currentTouch).position;
				ct.y = Screen.height - ct.y;
				
				if(UI.boostControl.Contains(ct))
				{
					GameObject.Find("Player").SendMessage("Boost");
				}
			}
		}
	}

Yup, you capture multiple touches with a for loop. here is one for your moving and jumping thing. notice it jumps on begin only and moves on stationary. THats so he’ll only jump once each time you press but continue to move as long as its pressed…

if (Input.touchCount >0)
for (int i = 0; i < Input.touchCount; ++i)
{
	RaycastHit2D hit = Physics2D.Raycast(GUICam.ScreenToWorldPoint(Input.touches*.position), Vector2.zero);*

_ if (Input.touches*.phase == TouchPhase.Began)_
_
{*_

* switch (hit.collider.name){*
* case “JumpButton”:*
* // jump*
* break;*

* default:*
* break;*
* }*
* }*

_ if (Input.touches*.phase == TouchPhase.Stationary)
{
switch (hit.collider.name){*_

* case “GoLeft”:*
* //move left*
* break;*
* case “goRight”:*
* //go right*
* break;*
* default:*
* break;*
* }*
* }*
}