0
0
Unityframework~10 mins

Mouse input (GetMouseButton, position) in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mouse input (GetMouseButton, position)
Start Frame
Check Mouse Button State
GetMouseButton(0) pressed?
NoNo Action
Get Mouse Position
Use Position for Action
End Frame / Repeat
Each frame, the program checks if the mouse button is pressed. If yes, it reads the mouse position and uses it for actions like clicking or dragging.
Execution Sample
Unity
void Update() {
  if (Input.GetMouseButton(0)) {
    Vector3 pos = Input.mousePosition;
    Debug.Log($"Mouse at {pos}");
  }
}
This code checks if the left mouse button is held down and prints the current mouse position each frame.
Execution Table
FrameInput.GetMouseButton(0)Input.mousePositionActionOutput
1FalseN/ANo actionNo output
2True(150, 300, 0)Log mouse positionMouse at (150, 300, 0)
3True(152, 305, 0)Log mouse positionMouse at (152, 305, 0)
4FalseN/ANo actionNo output
5True(160, 310, 0)Log mouse positionMouse at (160, 310, 0)
6FalseN/ANo actionNo output
💡 Frames continue indefinitely; this trace shows sample frames where mouse button is pressed or not.
Variable Tracker
VariableStartAfter Frame 1After Frame 2After Frame 3After Frame 4After Frame 5After Frame 6
Input.GetMouseButton(0)FalseFalseTrueTrueFalseTrueFalse
Input.mousePositionN/AN/A(150, 300, 0)(152, 305, 0)N/A(160, 310, 0)N/A
Key Moments - 2 Insights
Why is Input.mousePosition 'N/A' when GetMouseButton(0) is False?
When the mouse button is not pressed, the code does not read or use Input.mousePosition, so it remains unused or 'N/A' in the trace (see frames 1,4,6 in execution_table).
Does GetMouseButton(0) detect clicks or holds?
GetMouseButton(0) returns true as long as the left mouse button is held down, not just on the initial click. This is why frames 2 and 3 both show True (see execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is Input.GetMouseButton(0) at Frame 4?
ATrue
BFalse
CN/A
DDepends on mouse position
💡 Hint
Check the 'Input.GetMouseButton(0)' column for Frame 4 in the execution_table.
At which frame does the mouse position first get logged?
AFrame 1
BFrame 4
CFrame 2
DFrame 6
💡 Hint
Look for the first frame where 'Action' is 'Log mouse position' in the execution_table.
If the mouse button is never pressed, what will be the output?
ANo output
BMouse positions logged every frame
CError because mousePosition is undefined
DOutput only on first frame
💡 Hint
Refer to frames where GetMouseButton(0) is False and see the 'Output' column in execution_table.
Concept Snapshot
Unity Mouse Input:
- Use Input.GetMouseButton(0) to check if left mouse button is held.
- Use Input.mousePosition to get current mouse position.
- Typically checked inside Update() each frame.
- Returns true while button is held, not just clicked.
- Mouse position is in screen pixels (Vector3).
Full Transcript
This visual trace shows how Unity checks mouse input each frame. The program uses Input.GetMouseButton(0) to see if the left mouse button is pressed. If it is, it reads Input.mousePosition to get the mouse's screen coordinates. The example code logs the mouse position only when the button is held. The execution table shows frames where the button is pressed or not, and the corresponding mouse positions and outputs. Variables track the button state and position over time. Key moments clarify that mouse position is only read when the button is pressed, and that GetMouseButton(0) detects holding, not just clicks. The quiz tests understanding of these steps.