Today's blog is about How to take touch input in our games.
This blog is continuous to Input in our games (so if haven’t read the previous blogs I recommend to read those as well to understand overall concept of Inputs in game using: Mouse, Keyboard and Touch).
Basically, Touch inputs are used for Mobile devices, so whenever your target audience is mobile users you have to work with touch input feature of unity. You can visit unity official site for an insight of touch input mechanism.
Before we start, I will also recommend you to Download android sdk and java jdk.
There is different procedure for different unity versions.
To know how to setup your sdk and jdk in unity please follow the below links.
(Links:
So, let’s start with today’s tutorial, today we are going to understand three touch features which are:
· Number of touches on screen
· Touch position
· Touch Phases
1. Number of touches on screen
Step 1: Create a unity scene and add Three panels.
Inside panel one and two add a text, and in panel three add a dropdown field.
Next Create an EmptyGameobject and attach a script to it, name your Emptygameobject as per your need, I will name it “ScriptHandler”.
Your Hierarchy window will look something like this:
And Game window Like this:
Step 2: Write the following code:
Create four global variables (mylabel, mytext, mydropdown and int i).
Drag and drop your gameobject as shown below:
The code is in update function, so it will keep on running until the touch input is more than 0,so I have used Variable i to make sure each input runs only once.
Next on line number 17 there is an if condition to check the i value and the dropdown field value. If the condition is true
Than the mylabel text is changed to “Number of finger touched”
And mytext text will be changed to number of fingers touching device.
2. Touch position
To check your current finger touch position on screen simply write the below code:
This set of line are in continuous to previous if statement.
If you change the value of dropdown field to 1 this part of code will be executed.
3. Touch Phases
To know the current phase of touch input simply write the below attached code:
This set of line are in continuous to previous else if statement.
If you change the value of dropdown field to 2 this part of code will be executed.
Unity provides Five types of touch phase:
For more detail on unity touch phase you can check there official site: https://docs.unity3d.com/ScriptReference/TouchPhase.html
Output Window from android Device:
That's all for today this post is open to edit and comment.
Post A Comment:
0 comments: