Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • Types of Mouse Button Input
  • Using the Input Manager

Was this helpful?

  1. Interaction

Mouse Controls

Use a script and the Input class to determine what mouse button is pressed:

Types of Mouse Button Input

Input.GetMouseButton() - Returns true each frame the button within the parentheses is pressed. This is great for clicking and dragging items and drawing.

Input.GetMouseButtonDown() - Returns true only on the first frame the button within the parentheses is pressed. This is best for switches and buttons.

Input.GetMouseButtonUp() - Returns true on the frame the button within the parentheses is released.

Inside the parentheses, you want to use an integer that represents the mouse button:

0 - Left Mouse Button

1 - Right Mouse Button

2 - Center / Pressing the Scroll Button

Since the code is to see if a key is pressed or not, the code is generally used in if statements.

Examples:

if (Input.GetMouseButton(0))
{
     // Something to do when the left mouse button is held down
}

if (Input.GetMouseButtonDown(1))
{
     // Something to do when the player right-clicks
}

Common Issue: The object that should continuously move only moves a tiny bit and only when the player continues to tap the mouse button. Solution: You might be using GetMouseButtonDown() which only runs for one frame. Try using GetMouseButton() instead.

Using the Input Manager

If you set up the Input Manager, track mouse movement with Input.GetAxis("Mouse X") and Input.GetAxis("Mouse Y").

PreviousKeyboard ControlsNextIntro to Scripts

Last updated 4 years ago

Was this helpful?