0
0
Unityframework~10 mins

Touch input basics in Unity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the screen is touched.

Unity
if (Input.[1] > 0) {
    Debug.Log("Screen touched");
}
Drag options to blanks, or click blank then click option'
AtouchPhase
Btouches
CtouchCount
DtouchPosition
Attempts:
3 left
💡 Hint
Common Mistakes
Using Input.touches instead of Input.touchCount
Using Input.touchPhase which is not a property of Input
2fill in blank
medium

Complete the code to get the position of the first touch on the screen.

Unity
Vector2 touchPos = Input.touches[[1]].position;
Drag options to blanks, or click blank then click option'
A0
B1
C-1
DtouchCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 as index
Using touchCount as index which is out of range
3fill in blank
hard

Fix the error in the code to detect if the first touch just began.

Unity
if (Input.touches[0].[1] == TouchPhase.Began) {
    Debug.Log("Touch started");
}
Drag options to blanks, or click blank then click option'
AtouchCount
Bphase
CdeltaPosition
Dposition
Attempts:
3 left
💡 Hint
Common Mistakes
Using position instead of phase
Using touchCount which is not a property of Touch
4fill in blank
hard

Fill both blanks to create a dictionary mapping fingerId to touch position for all touches.

Unity
var touchPositions = new Dictionary<int, Vector2>();
for (int i = 0; i < Input.touchCount; i++) {
    var touch = Input.touches[i];
    touchPositions[touch.[1]] = touch.[2];
}
Drag options to blanks, or click blank then click option'
AfingerId
Bposition
Cphase
DdeltaPosition
Attempts:
3 left
💡 Hint
Common Mistakes
Using phase or deltaPosition instead of position
Using touchCount instead of fingerId
5fill in blank
hard

Fill all three blanks to detect a swipe by checking if the touch moved more than 50 pixels.

Unity
if (Input.touchCount > 0) {
    Touch touch = Input.touches[[1]];
    if (touch.phase == TouchPhase.[2]) {
        if (touch.[3].magnitude > 50) {
            Debug.Log("Swipe detected");
        }
    }
}
Drag options to blanks, or click blank then click option'
A0
BMoved
CdeltaPosition
DBegan
Attempts:
3 left
💡 Hint
Common Mistakes
Using phase Began instead of Moved
Using position instead of deltaPosition
Using wrong touch index