0
0
Unityframework~20 mins

Mouse input (GetMouseButton, position) in Unity - Mini Project: Build & Apply

Choose your learning style9 modes available
Mouse input (GetMouseButton, position)
📖 Scenario: You are creating a simple Unity program that detects when the user clicks the left mouse button and shows the position of the mouse pointer on the screen.
🎯 Goal: Build a Unity script that checks if the left mouse button is pressed and prints the mouse position coordinates to the console.
📋 What You'll Learn
Create a Unity C# script named MouseInput.
Use Input.GetMouseButton(0) to detect left mouse button press.
Get the mouse position using Input.mousePosition.
Print the mouse position to the console when the button is pressed.
💡 Why This Matters
🌍 Real World
Detecting mouse clicks and positions is essential for interactive games and applications where the user controls objects or navigates menus.
💼 Career
Understanding mouse input handling is a fundamental skill for Unity developers working on game mechanics, UI interactions, and user experience design.
Progress0 / 4 steps
1
Create the MouseInput script and define Update method
Create a C# script called MouseInput and add an empty Update() method inside the MonoBehaviour class.
Unity
Need a hint?

The Update() method runs every frame. You will add mouse input code inside it.

2
Add a boolean variable to check left mouse button press
Inside the Update() method, create a boolean variable called isLeftButtonPressed and set it to Input.GetMouseButton(0).
Unity
Need a hint?

Input.GetMouseButton(0) returns true if the left mouse button is pressed.

3
Get the mouse position when left button is pressed
Inside the Update() method, add an if statement that checks if isLeftButtonPressed is true. Inside the if, create a variable called mousePos and set it to Input.mousePosition.
Unity
Need a hint?

Input.mousePosition gives the current position of the mouse pointer in screen pixels.

4
Print the mouse position to the console
Inside the if block, add a Debug.Log statement that prints the text "Mouse Position: " followed by the mousePos variable.
Unity
Need a hint?

Debug.Log prints messages to the Unity Console window.