Unity - Multiple Touches on Mobile Device [Input.GetTouch]

preview_player
Показать описание
In this tutorial I explain how to store multiple touches within a custom list to then spawn objects that follow the movement of each touch.
============
In this video, we cover:
+ 0:30 - Setting Up A Custom List Script
+ 1:25 - Creating a Script to Detect Multiple Touches
+ 2:20 - Creating a While Loop to cycle between touches
+ 2:55 - The three different touch phases we will be using
+ 3:30 - Detecting Touch Start, End and Move
+ 4:10 - Testing our touches on a mobile device
+ 4:50 - Creating a function to spawn our circle prefabs
+ 6:15 - Adding our touches to our list
+ 6:45 - Searching our list and removing our touch after it ends
+ 8:08 - Moving our circle when the touch is moved

COPY & PASTE CODE FROM THIS TUTORIAL:

UNITY DEVELOPER REMOTE APP:

RELATED VIDEOS REGARDING SCREEN SPACE CONVERSION:

MORE TUTORIALS:
Рекомендации по теме
Комментарии
Автор

This channel is absolutely, crazily underrated. This man deserves so much more than he gets. The man has a website with an attractive interface for his videos for simplicity! And his tutorials skip all the bs! Thank you!

brandonhohn
Автор

After trying for 12 hours to make it my own way, and I almost made it :P... I came back to this tutorial, is much cleaner and it provides me with a position.
Thank you very much for taking the time to post this.


It got stuck when using "while", so I replaced it with a "for" instead. Not sure why, it happened to me before, maybe because of the version that I'm using, or maybe Unity isn't "while" friendly altogether.


Cheers, thanks again.

stefanstoicescu
Автор

Thank you so much! This is one of the only tutorials in all of YouTube that teaches about multitouch and fingerId

miceonvenus
Автор

This is amazing i was searching for this from past 3 days and you just did it in about 10 min amazing you save me man amazing work just simple and clean You got a follower, I realize other just make a video and didn't explain stuff as clearly as you do. Keep up the good work man. Thumb up

usmanarshad
Автор

Keep it up dude! I know for a fact you'll get the love you deserve sooner than later. Maybe a powerups video next?

bennettgodinho-nelson
Автор

Very brilliant and clearly explained! You saved me at least 2 days.

syu
Автор

im working on the version 4 and im trying to figure out how it worked back in those days (cant use anything new cuz my actual pc at the momment)

Nekotico
Автор

This was such a great tutorial! Thank you so much. I actually made mine in 3D, so I made a few modifications to your script. I'll drop it here in the comments in case anyone wants this result but in 3D :)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class multipleTouch : MonoBehaviour {
public GameObject circle;
public List<touchLocation> touches = new List<touchLocation>();

void Update () {
int i = 0;
while(i < Input.touchCount){
Touch t = Input.GetTouch(i);
if(t.phase == TouchPhase.Began)
{
Debug.Log("touch began");
touches.Add(new touchLocation(t.fingerId, createCircle(t)));
}
else if(t.phase == TouchPhase.Ended)
{
Debug.Log("touch ended");
touchLocation thisTouch = touches.Find(touchLocation => touchLocation.touchId == t.fingerId);
Destroy(thisTouch.circle);

}
else if(t.phase == TouchPhase.Moved)
{
Debug.Log("touch is moving");
touchLocation thisTouch = touches.Find(touchLocation => touchLocation.touchId == t.fingerId);
=
}
++i;
}
}

GameObject createCircle(Touch t){
GameObject c = Instantiate(circle) as GameObject;
c.name = "Touch" + t.fingerId;
c.transform.position =
return c;
}

Vector3 getTouchPosition(Vector2 touchPosition)
{
return Vector3(touchPosition.x, touchPosition.y, 20f));
}
}

If you want the circle (or whatever gameobject you have) to be bigger/smaller in the screen, just modify the "20f" in the getTouchPosition method.
The other script stays the same though. Hope this helps!

jonathangarciarodriguez
Автор

Neat and clean tutorial and must-know information . Thanks a lot!

brainy
Автор

Great information. Not that it matters in the grand scheme of things but I'm confused about the way you format code, particularly around casing and line breaks between functions. Did you start with a language other than c#?

danieljayne
Автор

It's a good tutorial, but I can't finish it as I can't make out what's in the rest of the line of code you paste in at 5:51. What is it? :) Thanks.

Trickerik
Автор

Nice video. I'm still way more comfortable with the old input system than the new one, and this explains multi-touch the old way!

You essentially built a dictionary here from the list plus the touchLocation class. Maybe cleaner to just use Dictionary<int, GameObject> or Dictionary<Touch, GameObject> ?

MikeArcuri
Автор

Is there a way to make the touch and release a timer?

dejoncage
Автор

in case I want to create different objects with each tap. What do I do?

pedrohenrique-rciv
Автор

I would have thought a for loop would make more sense than while, and is very quick to implement with the for snippet. Otherwise, very useful series of tutorials.

lexxynubbers
Автор

I feel like at 4:02, you just reinvented the concept of a for loop!

gregoryfenn
Автор

Thanks for this video, it's exactly what i was searching for :D

alextreme
Автор

The movements come out the other way around, I move it to the left and it goes to the right, I move it up and it goes down

diegoRAM_
Автор

Thank you so much for the tutorial!
Will this work with a touchpad connected to a PC through USB or it only works on mobile platforms?

IbizanHound
Автор

How to code the "getTouchPosition" for perspective camera?

noraholmberg