- [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
its been years since I've looked at anything programming but here I am at 5:30 am lol
What is the diffrence between OnMouseDown and Raycasting from screen click position? Perfomance?
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.
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
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; } }
Doesn't work on 2d, does it only work on 3d collieers
Please what is the difference between transform.forward and Vector3.forward?
Why it is not inside the update()?
I have a question: there is any function that detects if the click of the mouse occurs outside of the object?
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
Nice when I get my new laptop Amma try these stuffs
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
Thanks
Is der also a more aesy way to make phone games