- Home /
How can i use touch input on android 2d game in unity?
Hello, First i wanna say that i'm a begginer in Unity. I want to make a 2d android game with touch inputs for the university. I read the Unity tutorials, but i don;t understand how to script. For example, i don't understand how to script for one touch ( when clicking the arrow for the next level) and when you take an object and move it in another place. Thank you, and any advice is welcomed.
Answer by Denvery · Jan 20, 2015 at 07:04 PM
Script for one touch may be as below:
void Update()
{
if (Input.touches > 0) //if any finger are on the screen
{
Touch touch = Input.touches[0];
Rect rect = new Rect(0, 0, 150, 150); //rect of your Arrow for the next level
if (rect.Contains(touch.position))
{
//Do actions
}
}
}
Script for dragging can be looks like there:
GameObject thing;
void Update()
{
if (Input.touches > 0) //if any finger are on the screen
{
Touch touch = Input.touches[0];
if (touch.phase == TouchPhase.Began)
{
Rect rect = new Rect(0, 0, 150, 150); //rect of your thing that you want to catch
if (rect.Contains(touch.position))
{
thingIsCaptured = true;
}
}
if (touch.phase == TouchPhase.Moved)
{
if (thingIsCaptured)
{
thing.tranform.Translate(touch.deltaPosition);
}
}
if (touch.phase == TouchPhase.Ended)
{
thingIsCaptured = false;
}
}
}
You can learn more here: http://docs.unity3d.com/ScriptReference/Input.html
Your answer
Follow this Question
Related Questions
I can't change the sprite using the Touch class 0 Answers
Touch Input madness 2 Answers
Three finger tap? 1 Answer
Processes two touches instead of one 0 Answers
How can I tell if the device I am on supports touch? 4 Answers