0
0
Unityframework~10 mins

Touch input basics in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Touch input basics
Start Frame
Check Touch Count > 0?
NoNo Touch Detected
Yes
Get Touch Data (position, phase)
Process Touch Based on Phase
Respond to Touch (move, tap, etc.)
End Frame / Wait Next Frame
Each frame, Unity checks if the screen is touched. If yes, it reads touch details and reacts based on touch state.
Execution Sample
Unity
void Update() {
  if (Input.touchCount > 0) {
    Touch touch = Input.GetTouch(0);
    if (touch.phase == TouchPhase.Began) {
      Debug.Log($"Touch started at {touch.position}");
    }
  }
}
This code checks if the screen is touched and logs the position when a new touch begins.
Execution Table
StepInput.touchCountTouch IndexTouch.phaseConditionActionOutput
10--0 > 0?NoNo touch detected, nothing happens
210Began1 > 0?YesTouch detected, check phase
310Beganphase == Began?YesLog touch start position
410Movedphase == Began?NoNo log, touch moved
50--0 > 0?NoNo touch detected, nothing happens
💡 When touchCount is 0, no touch is detected and code skips touch processing.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
Input.touchCount01110
touch.phase-BeganBeganMoved-
Condition (touch.phase == Began)-TrueTrueFalse-
Key Moments - 3 Insights
Why does the code check if Input.touchCount > 0 before getting touch data?
Because if there are no touches, trying to get touch data causes errors. The execution_table row 1 shows no touch detected when touchCount is 0.
What does TouchPhase.Began mean and why is it important?
TouchPhase.Began means the finger just touched the screen. The code reacts only at this phase to detect new touches, as shown in execution_table rows 2 and 3.
Why doesn't the code log anything when the touch phase is Moved?
Because the condition checks for Began phase only. When phase is Moved (row 4), the condition is false, so no log happens.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is Input.touchCount at Step 2?
A1
B0
C2
D-
💡 Hint
Check the Input.touchCount column at Step 2 in the execution_table.
At which step does the code log the touch start position?
AStep 4
BStep 1
CStep 3
DStep 5
💡 Hint
Look for the Output column mentioning logging in the execution_table.
If Input.touchCount was never checked, what would happen when no touches exist?
ACode runs normally
BError occurs trying to get touch data
CTouch phase is always Began
DTouchCount becomes negative
💡 Hint
Refer to key_moments about why checking touchCount is necessary.
Concept Snapshot
Unity Touch Input Basics:
- Check Input.touchCount > 0 each frame
- Use Input.GetTouch(index) to get touch info
- Touch.phase tells touch state (Began, Moved, Ended)
- React to phases to handle touch events
- Always check touchCount before accessing touches
Full Transcript
In Unity, touch input is checked every frame by looking at Input.touchCount. If it is greater than zero, the program reads the first touch using Input.GetTouch(0). The touch has a phase property that tells if the touch just began, moved, or ended. The example code logs the position only when the touch phase is Began, meaning a new finger just touched the screen. If no touches exist, the code skips processing to avoid errors. This flow ensures safe and correct touch input handling.