Challenge - 5 Problems
Touch Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Detecting a single touch phase
What will be the output of this Unity C# script snippet when the user touches the screen and holds their finger still?
Unity
void Update() {
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Stationary) {
Debug.Log("Finger is still on screen");
}
}
}Attempts:
2 left
💡 Hint
Think about what TouchPhase.Stationary means in Unity's touch system.
✗ Incorrect
TouchPhase.Stationary means the finger is touching the screen but not moving. The code checks for this phase every frame, so it prints the message continuously while the finger is held still.
❓ Predict Output
intermediate1:00remaining
Counting touches on screen
What will be the value of the variable 'touchCount' after this code runs if the user has two fingers touching the screen?
Unity
int touchCount = Input.touchCount;Attempts:
2 left
💡 Hint
Input.touchCount returns the number of fingers currently touching the screen.
✗ Incorrect
Input.touchCount returns the number of active touches. If two fingers are on the screen, it returns 2.
🔧 Debug
advanced2:00remaining
Why does this touch position code cause an error?
This code tries to print the position of the first touch. Why does it cause an error when no fingers are touching the screen?
Unity
void Update() {
Vector2 pos = Input.GetTouch(0).position;
Debug.Log(pos);
}Attempts:
2 left
💡 Hint
Think about what happens if you ask for a touch index that does not exist.
✗ Incorrect
Input.GetTouch(0) throws an ArgumentOutOfRangeException if there are no touches because index 0 is invalid.
🧠 Conceptual
advanced1:30remaining
Understanding TouchPhase.Moved behavior
Which statement best describes when TouchPhase.Moved is detected in Unity touch input?
Attempts:
2 left
💡 Hint
Think about what 'Moved' means in the context of touch input.
✗ Incorrect
TouchPhase.Moved means the finger is touching the screen and has changed position since the last frame.
❓ Predict Output
expert2:30remaining
Output of multi-touch gesture detection code
What will be printed by this Unity C# code if the user touches the screen with two fingers and moves both fingers?
Unity
void Update() {
if (Input.touchCount == 2) {
Touch touch0 = Input.GetTouch(0);
Touch touch1 = Input.GetTouch(1);
if (touch0.phase == TouchPhase.Moved && touch1.phase == TouchPhase.Moved) {
Debug.Log("Two finger move detected");
} else {
Debug.Log("Not both fingers moved");
}
}
}Attempts:
2 left
💡 Hint
Check the condition that requires both fingers to have moved.
✗ Incorrect
The code prints "Two finger move detected" only if both fingers have the Moved phase in the same frame.