- Home /
Detect last Touch
Hi, I'm making a mobile joystick from scratch, but when i try to use 2 joystick at the same time i don't know how to detect what touch is used by the first joystick and what by the second... can you help me?
Answer by DevManuel · Aug 11, 2021 at 09:56 AM
Hi @PhixDevelop , When you have 2 joysticks and one is on the left side and the other joystick ist on the right side, you could check if the position.x is on the left side or on the right side of the screen.
Here is a little code which may help you:
private Vector3 position;
Vector3 joy1; // left joystick
Vector3 joy2; // right joystick
private float width;
void Awake()
{
width = (float)Screen.width / 2.0f;
}
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector2 pos = touch.position;
pos.x = (pos.x - width) / width;
pos.y = (pos.y - height) / height;
position = new Vector3(-pos.x, pos.y, 0.0f);
if(position.x < width/2){
// left joystick
joy1 = position;
}else{
// right joystick
joy2 = position;
}
if (Input.touchCount == 2)
{
touch = Input.GetTouch(1);
Vector2 pos = touch.position;
pos.x = (pos.x - width) / width;
pos.y = (pos.y - height) / height;
position = new Vector3(-pos.x, pos.y, 0.0f);
if(position.x < width/2){
// left joystick
joy1 = position;
}else{
// right joystick
joy2 = position;
}
}
}
}
This script only check the two finger touch positions. Then it checks if the finger is on the right or on the left side of the screen.
Thanks for replying, but in these days I found a solution with Interfaces (IPointerDownHandler, ect...) and it works as I wanted.
Your answer
Follow this Question
Related Questions
How to destroy object by touch? 2 Answers
mouse input to touch input help please 0 Answers
Swipe menu, problem! 0 Answers
Is it possible to have the Unity TouchScreenKeyboard permanently open? 0 Answers
Inertia Scrolling Touch 1 Answer