- Home /
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:
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'd advise you have all game-play controls in one script like most examples.
Answer by Raimi · Aug 11, 2014 at 04:45 PM
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 :)
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");
}
}
}
}
So Im guessing I need to use a 'foreach' loop, as well as use an array to store clicks? Is that the only way I can do it? Also, I know 1 script would be more efficient, but if i can do it over 3, that would be more preferable as it is already set up that way.
Alright I can see that one working, will test tomorrow. Do you know if that will allow for me to do two touches at the same time? IE hold down the button for moving right, and then also tap while still holding the button?
Im struggling with this one because each of my buttons is a Sprite with a Box Collider 2D that just moves with the camera (that part was really simple) and that all works fine. I am not able to use the .Contains function with a Unity GameObject such as a Box Collider in order to detect the press. I am not willing to do this another way than how I have it set up with the Sprites and Box Colliders, as I have made it work and have really come to like the system.
Answer by RayJr · Aug 21, 2015 at 11:46 AM
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[i].position), Vector2.zero);
if (Input.touches[i].phase == TouchPhase.Began)
{
switch (hit.collider.name){
case "JumpButton":
// jump
break;
default:
break;
}
}
if (Input.touches[i].phase == TouchPhase.Stationary)
{
switch (hit.collider.name){
case "GoLeft":
//move left
break;
case "goRight":
//go right
break;
default:
break;
}
}
}
Your answer
Follow this Question
Related Questions
Problem with multi touch[solved] 0 Answers
Android 2D multitouch joystick + buttons 0 Answers
MultiTouch Help(C#) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers