Detecting Mouse Clicks

Tutorial

·

Beginner

·

+10XP

·

5 mins

·

(2003)

Unity Technologies

Detecting Mouse Clicks

How to detect mouse clicks on a Collider or GUI element.

This tutorial is included in the Beginner Scripting project.

Previous: GetAxis

Next: GetComponent

1. OnMouseDown

MouseClick

using UnityEngine;
using System.Collections;

public class MouseClick : MonoBehaviour
{

    private Rigidbody rb;

    private void Awake()
    {
        rb = GetComponent<Rigidbody>();
    }

    void OnMouseDown ()
    {
        rb.AddForce(-transform.forward * 500f);
        rb.useGravity = true;
    }
}

Complete this tutorial