- Home /
Multitouch Area
I'm brazilian and my english is basic.
I have two types of input on my game (racing game):
•Vertical input (Two button that move forward and backward);
•Horizontal input (Horizontal slider that rotates the car);
But if I press the Slider and the Button, the touch does not work properly.
How can I create a "multitouch area", like those "touch areas" in FPS game, in which the left part of screen moves the player, and the right part of the screen moves the aim camera.
Part of my code:
//Vertical Input
if (Input.GetMouseButton(0) && GUI.RepeatButton( Rect( Screen.width - Screen.width/10, Screen.height - Screen.width /5, Screen.width /12, Screen.width /12 ), cima))
{
VerticalGUI = 1;
}
else if (Input.GetMouseButton(0) && GUI.RepeatButton( Rect( Screen.width - Screen.width/10, Screen.height - Screen.width /10, Screen.width /12, Screen.width /12 ), baixo))
{
GUI.RepeatButton( Rect( Screen.width - Screen.width/10, Screen.height - Screen.width /5, Screen.width /12, Screen.width /12 ), cima);
VerticalGUI = -1;
}
else
{
VerticalGUI = 0;
}
//Horizontal Input
if (Input.GetMouseButton(0))
{
HoriGUI = GUI.HorizontalSlider (Rect (Screen.width /25, Screen.height - Screen.height /10, Screen.width /3.8, Screen.height /16), HoriAll, -30.0, 30.0);
}
else
HoriGUI = GUI.HorizontalSlider (Rect (Screen.width /25, Screen.height - Screen.height /10, Screen.width /3.8, Screen.height /16), 0, -30.0, 30.0);
This is a collection of the comments I posted on this question : http://answers.unity3d.com/questions/373516/brother-in-arms-style-joystick-and-fire-button.html
Normally you check for touches within certain rectangles on the screen. If there is a touch on the left, use that touch in move calculations; if there is a touch on the right, use that touch for firing calculations.
I'm just searching a solution to make a GUI compatible with different mul$$anonymous$$ch areas. But now I know that GUI does not have support to $$anonymous$$ul$$anonymous$$ch.
I'm looking for an Android game, not for iOS devices.
Answer by AlucardJay · Jan 09, 2013 at 01:17 PM
To expand on my first comment, what normally happens is first you define rectangles that represent areas on the screen :
// set control input zones - joystick on left, fire button on right
joystickRect = new Rect(0, 0, Screen.width * 0.5, Screen.height * 1.0);
fireBtnRect = new Rect(Screen.width * 0.5, 0, Screen.width, Screen.height * 1.0);
then when you get your touch input, check if it is in one of the areas :
if ( joystickRect.Contains( touch.position ) )
{
// do joystick stuff
}
else if ( fireBtnRect.Contains( touch.position ) )
{
// do firing stuff
}
This is the script I learned from : http://wiki.unity3d.com/index.php/Tap_to_Move_Drag_to_Look_iPhone
What to have first is some kind of input manager :
// TOUCH
var count : int = Input.touchCount;
for (var i : int = 0; i < count; i++)
{
var touch : Touch = Input.GetTouch (i);
if (touch.phase == TouchPhase.Began)
OnTouchBegan(touch.fingerId, touch.position);
else if (touch.phase == TouchPhase.Moved)
OnTouchMoved(touch.fingerId, touch.position);
else if (touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended)
OnTouchEnded(touch.fingerId);
else if (touch.phase == TouchPhase.Stationary)
OnTouchStationary(touch.fingerId);
}
that passes the touch input to one of 4 functions. :
// -- Control Functions --
function OnTouchBegan(fingerId : int, pos : Vector2)
function OnTouchEnded(fingerId : int)
function OnTouchMoved(fingerId : int, pos : Vector2)
function OnTouchStationary(fingerId : int)
In OnTouchBegan, first it checks what area of the screen (rect) the touch started in, and stores a reference to that finger ID :
function OnTouchBegan(fingerId : int, pos : Vector2)
{
if (leftFingerId == -1 && joystickRect.Contains(pos))
{
leftFingerId = fingerId;
leftFingerStartPoint = leftFingerCurrentPoint = pos;
}
else if (rightFingerId == -1 && fireBtnRect.Contains(pos))
{
rightFingerStartPoint = rightFingerCurrentPoint = pos;
}
}
OnTouchEnded checks which fingerID has been ended, and removes the reference to that finger :
function OnTouchEnded(fingerId : int)
{
if (fingerId == leftFingerId)
{
leftFingerId = -1;
// reset Joystick vars
leftFingerCurrentPoint = leftFingerStartPoint;
joystickOffset = Vector2.zero;
playerInput = Vector2.zero;
}
else if (fingerId == rightFingerId)
{
rightFingerId = -1;
}
}
OnTouchMoved checks the finger ID, and calculates the change in position since the last frame :
function OnTouchMoved(fingerId : int, pos : Vector2)
{
if (fingerId == leftFingerId)
leftFingerCurrentPoint = pos;
else if (fingerId == rightFingerId)
{
rightFingerCurrentPoint = pos;
firingTimer += Time.deltaTime;
}
}
That is just the basics, and this is alot of information to absorb!
This is alot of information, do you see what is happening to check if a touch is only in a certain area (Rect)?
But the question is $$anonymous$$ul$$anonymous$$ch Area , not take inputs from GUI. And this is how $$anonymous$$ul$$anonymous$$ch Areas are done. Normally your GUI elements are rendered afterwards in positions based on the calculations as advised above, look at any iOS input script. Also I'm fairly sure Unitys GUI doesn't work on iOS, that's why you need to use something like NGUI or UIToolkit. Perhaps you should be looking at NGUI : http://u3d.as/content/tasharen-entertainment/ngui-next-gen-ui/2vh
Your answer
Follow this Question
Related Questions
How to identify touches via area 1 Answer
Issues with using multitouch and event trigger on 2 joysticks 1 Answer
Touch ID ????? 1 Answer
How to display Swipe Marks on iPhone 1 Answer