- Home /
Detecting Multiple Touches on same frame IOS
Hullo Unity world,
I am trying to script code that allows a user to press multiple guiTextures at the same time. Example- The guiTextures are up, left and right. I would like to be able to go left and up at the same time but I am struggling to code in my textures to the code below? :-
var ball : Transform;
var ArrowLeft : GUITexture;
var ArrowRight : GUITexture;
var ArrowUp : GUITexture;
function Update()
{
var count : int = Input.touchCount;
for(var i: int = 0;i < count; i++)//for multi touch
{
var touch : Touch = Input.GetTouch(i);
if(guiTexture.HitTest(touch.position) && touch.phase == TouchPhase.Began)
{
HOW WOULD I IMPLEMENT MY THREE ACTIONS INTO HERE?? :/
}
}
}
Answer by buxton4life · Aug 28, 2012 at 08:33 PM
var ArrowLeft : GUITexture;
var ArrowUp : GUITexture;
function Update(){
var count : int = Input.touchCount;
for(var i: int = 0;i < count; i++)
{
var touch : Touch = Input.GetTouch(i);
if(ArrowLeft.HitTest(touch.position))
{
// code to go left
}
if(ArrowUp.HitTest(touch.position))
{
// code to go up
}
}
}
Answer by ScroodgeM · Aug 27, 2012 at 07:03 PM
methinks that changing Input.mousePosition to evt.position in first example will solve your issue
Yes thats right, but I was wondering how to make the second piece of code work like the first but the second would allow two textures to be used at the same time? Any ideas? Thank You.
your question is unclear for me a little.
if(guiTexture.HitTest(touch.position) && touch.phase == TouchPhase.Began)
which texture do you checking here? and how do you want to implement 3 actions based on one texture? is it one texture with all buttons at once or something?
No sorry, I have three different textures in my scene. One that makes the ball go up and two which make it go left and right. I'm stuck on how to get these three to work in array format so that two of them can be used at the same time. So for example the user could press up and right and the ball would go diagonally North East, though when I try pressing two buttons with the first bit of code it only allows one at a time. Sorry it's hard to explain ha. Thanks again for the reply.
I've never used HitTest before, but maybe try like this:
for(var i: int = 0;i < count; i++)
{
var touch : Touch = Input.GetTouch(i);
if(ArrowLeft.HitTest(touch.position))
{
// code to go left
}
if(ArrowUp.HitTest(touch.position))
{
// code to go up
}
}
also, keep in $$anonymous$$d that TouchPhase.Began will only refer to the first frame that texture is hit, (which is why I removed it here, but it kind of depends on how your movement is implemented..)
Thats the one seth it works perfect. All I had to do was put
var count : int = Input.touchCount;
below the update function i'll post the your answer as an answer now. Thanks Again.
Your answer
Follow this Question
Related Questions
android touch input 1 Answer
Unity 2D Mobile Game Drawing Mechanic 0 Answers
touch position 2 Answers
Need Help With Unity iOS "touch" Code 2 Answers