Main

C# OnMouseDown in Unity! - Beginner Scripting Tutorial

Watch this video in context on Unity Learn: https://learn.unity.com/tutorial/onmousedown How to detect mouse clicks on a Collider or GUI element.

Unity

4 years ago

- [Narrator] OnMouse Down and its related functions, can detect a click on a collider or a GUI text element. In this example, we have a door object that has a box collider and a Rigidbody attached. The script that I've written has an OnMouse Down function and when that object is clicked, we will debug log, i.e. print to the console, "clicked on the door". So when I press Play, check my console and click on the door in the game, I get a debug log. Similarly we can make it do something more exciti
ng, for example, adding a force to the Door object when it's clicked. So in this example I'm adding a force in the inverse direction of its forward so the door will be fired away from its said axis, and I'm also switching on gravity so that it will fall down when it's clicked. It looks like this! (mouse clicking) (mouse clicking) (mouse clicking)

Comments

@piercezaman9507

its been years since I've looked at anything programming but here I am at 5:30 am lol

@VaustXIII

What is the diffrence between OnMouseDown and Raycasting from screen click position? Perfomance?

@JimmyHill5

Is there a way of setting a maximum distance from the object when clicking? For example using an FPS controller, you could use this script to open a door, but without a range check you'd be able to open the door from far away. Any help appreciated.

@TonyLovell

These videos never seem to spell out all that is actually required for their functionality to work. Here, the GameObject clicked on needs to have a collider whose isTrigger=true, Physics.queriesHitTriggers must be set to true, and the item clicked on needs a RigidBody. Also, this seems like a missed opportunity to discuss questions of what happens if multiple items are under the mouse, how to precude "clicking through walls", etc

@gregoryfenn1462

I've been using Unity for 2 years and never new about OnMouseDown, OnMouseEnter, OnMouseExit as special functions. I was always using some kind of Camera Raycast through mouse to hit colliders to run any functionality, but this is 100x easier! If anyone wants to test it out, you can use the following code as a component of a 3D collider (like a default cube or sphere) and play with it in a new unity project: public class MouseAction : UnityEngine.MonoBehaviour { void OnMouseDown() { transform.position += UnityEngine.Vector3.right; } void OnMouseExit() { transform.localScale *= 0.5f; } void OnMouseEnter() { transform.localScale *= 2; } }

@bevvy.bee9

Doesn't work on 2d, does it only work on 3d collieers

@omaralkaley222

Please what is the difference between transform.forward and Vector3.forward?

@WETDOGBR

Why it is not inside the update()?

@cristian4026

I have a question: there is any function that detects if the click of the mouse occurs outside of the object?

@DoomCatcher

Can this method be used universally with if statements for doing stuff click on any object w ith a rigidbody or only the objects this script is attached to? What a stupidly short tutorial short tutorials like these waste more of peoples time trying to figure stuff out than it saves

@devantehines5974

Nice when I get my new laptop Amma try these stuffs

@joshbasadre9673

i think the problem with the tutorial is that you use things outsikde the tutorial sobject or in other words insert something thaty is not teached from the subject you are talking about so yeah i hbope you make a new tutorial series but actually helpful

@kennymeesters9066

Is der also a more aesy way to make phone games