- Home /
How to multi touch on mobile game
I'm making a mobile game and is currently facing an issue with multi touch. The game character was controlled by using a joystick pack from Unity asset store. There is a power-up inside the game for players to collect by tapping on it. However, while I'm controlling the game character (Holding the joystick), I'm unable to tap the object at the same time but it works when I'm not using the joystick. How do I get two of them to work at the same time? Thanks in advance.
Here's the script for the game character:
public float moveSpeed = 2f;
public float forwardMovementSpeed = 3.0f;
public Rigidbody2D rb;
public Joystick joystick;
Vector2 movement;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
movement.y = joystick.Vertical;
}
void FixedUpdate()
{
rb.MovePosition(rb.position + new Vector2(forwardMovementSpeed * Time.fixedDeltaTime,
movement.y * moveSpeed * Time.fixedDeltaTime));
}
Here's the script for the power-up:
public GameObject LikeButton;
Collider2D col;
void Start()
{
col = GetComponent<Collider2D>();
}
public void Update()
{
if (Input.touchCount < 0)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
if (touch.phase == TouchPhase.Began)
{
Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
if (col == touchedCollider)
{
Destroy(gameObject);
}
}
}
}
Answer by RadonRaph · Nov 22, 2019 at 05:30 PM
Hello @lawrrennce, Unity store touches in an array. The first finger to touch the screen is at 0, second 1 etc. In your pickup script you only check the first finger ! so its on the joystick. To check all fingers you have juste to pass it in a loop like this:
public void Update()
{
if (Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
if (touch.phase == TouchPhase.Began)
{
Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
if (col == touchedCollider)
{
Destroy(gameObject);
}
}
}
}
}
Hope that help,
Raph :)
Hi @RadonRaph , thank you for taking ur time to explain to me. I tried what u suggested, but now whenever I touch anywhere on the screen, the objects got destroyed. I want to make it so that I can touch on the object's collider itself to destroy it. Any suggestions? Once again, thank you very much!
Its a problem from your collider, I don't know how your scene is made but what i will do is to compare distance of the touch position to the object like this:
public void Update()
{
if (Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
if (touch.phase == TouchPhase.Began)
{
if (Vector2.distance(transform.position, touchPosition) < $$anonymous$$yThresold){
Object is Touched !
}
}
}
}
}
If this don't work tell me i will find another solution :)
Sorry didn't saw your reply, but I managed to fix the problem already but thanks for your help though because of your first reply I finally know how does it work. :))
Your answer
![](https://koobas.hobune.stream/wayback/20220612215152im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Cross Platform Joystick Rotation 0 Answers
joystick connected 1 Answer