0
0
Unityframework~20 mins

Touch input basics in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Touch Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
        }
    }
}
ANo output, because touch.phase will never be Stationary
BPrints "Finger is still on screen" only when the finger is lifted
CPrints "Finger is still on screen" only once when the finger touches the screen
DPrints "Finger is still on screen" continuously while the finger is held still
Attempts:
2 left
💡 Hint
Think about what TouchPhase.Stationary means in Unity's touch system.
Predict Output
intermediate
1: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;
A2
B1
C0
DDepends on the device
Attempts:
2 left
💡 Hint
Input.touchCount returns the number of fingers currently touching the screen.
🔧 Debug
advanced
2: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);
}
AThe code runs fine even with no touches
BInput.GetTouch(0) throws an exception if there are no touches
CInput.touchCount is zero so position is Vector2.zero
DInput.GetTouch(0) returns null causing a NullReferenceException
Attempts:
2 left
💡 Hint
Think about what happens if you ask for a touch index that does not exist.
🧠 Conceptual
advanced
1:30remaining
Understanding TouchPhase.Moved behavior
Which statement best describes when TouchPhase.Moved is detected in Unity touch input?
AWhen the finger moves on the screen between frames
BWhen the finger is placed on the screen
CWhen the finger is lifted from the screen
DWhen the finger is held still on the screen
Attempts:
2 left
💡 Hint
Think about what 'Moved' means in the context of touch input.
Predict Output
expert
2: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");
        }
    }
}
A"Not both fingers moved" if both fingers moved
B"Two finger move detected" even if only one finger moved
C"Two finger move detected" if both fingers moved this frame
DNo output because Input.touchCount is never exactly 2
Attempts:
2 left
💡 Hint
Check the condition that requires both fingers to have moved.