C# OnMouseDown in Unity! - Beginner Scripting Tutorial

preview_player
Показать описание
Watch this video in context on Unity Learn:

How to detect mouse clicks on a Collider or GUI element.
Рекомендации по теме
Комментарии
Автор

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

TonyLovell
Автор

this video was insanely helpful, im following a tutorial and i havent been able to continue because it wasnt working, this video let me fix it insanely quickly and it works now ! thanks again

JennieDebruijn
Автор

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;
}
}

gregoryfenn
Автор

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

SameShiite
Автор

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.

JimmyHill
Автор

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

omaralkaley
Автор

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

RavenFelman
Автор

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

VaustXIII
Автор

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

DoomCatcher
Автор

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

bevvy.bee
Автор

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

joshbasadre
Автор

Nice when I get my new laptop Amma try these stuffs

dhines
Автор

Is der also a more aesy way to make phone games

kennymeesters
visit shbcf.ru