- Home /
How To Perform A Mouse Click On Game Object
Hello,
I would like the ability to click on an object in the game play.
Eg: user clicks on cube in the game, a dialog window then pops up, or an event is triggered.
I have no person controls or anything, just a camera.
--- Snippet of Scrrpt -----
function Update ()
{
if(Input.GetMouseButtonDown (0) && Clicked == false)
{
This works, but I can click anywhere on the screen....
Answer by Uriel_96 · Nov 21, 2010 at 02:12 AM
try this
if ( Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var select = GameObject.FindWithTag("select").transform;
if (Physics.Raycast (ray, hit, 100.0)){
select.tag = "none";
hit.collider.transform.tag = "select";
}
}
Okay, but what about when I want to deselect it or unclick it ... Thanks
good point, Im not completly sure but I will try to put an else something like this maybe: if (Physics.Raycast (ray, hit, 100.0)){}else{//Do what you want}
I've been looking for a way to detect if you've clicked on a game object and if so, destroy it. However, I'm getting some errors just trying to implicitly declare var hit and var ray to the different RaycastHit and Ray structs. I assume that there's no additional using statements needed here? Is there something I might be missing? I've copied the code above as is but it doesn't seem to like the ':' after hit and ray.
I am also facing same problem.When I mouse over on the object it move.But i want to move, when left mouse clicked.
kamal thapa april 6th, 2013
Answer by JakeElliott · Nov 21, 2010 at 02:21 AM
You can use the OnMouseDown function in a script component attached to your cube, as long as it has a collider: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDown.html
This is true, but beware that On$$anonymous$$ouseDown (and similar functions) don't work on mobile devices. Depending on your needs, this might not be a problem, but I thought it worth mentioning anyway.
Answer by jharri30 · Jan 17, 2014 at 03:08 AM
I was having some issues with Uriel's code, so I tweaked it a little to simplify it. Here is the solution that worked for me.
I created a cube which had a "Box Collider" on it by default and renamed it to "targetArea". I then added a script to it with the following update statement...
function Update ()
{
if ( Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100.0))
{
Destroy(GameObject.Find("targetArea"));
}
}
}
do you know how to do this but ins$$anonymous$$d when clicked will load a new scene?
Just replace Destroy(...); with the following sceneloading function.
using UnityEngine.Scene$$anonymous$$anagement; //add to top of script
Scene$$anonymous$$anager.LoadScene(string);
Long time later, but I am a bit confused by this code. While it tests to see if the ray hit anything, it doesn't seem to query for what it hit. If you only have one collider, then a hit has to be on that collider. But what if you have two or more? How do you know which one you hit?
Answer by AlisVitae · Jun 21, 2015 at 06:02 PM
Look at our tutorial! https://www.youtube.com/watch?v=HR_88quPMog
Your answer
Follow this Question
Related Questions
Mouse movement and click effect 1 Answer
Raycast hit not detected on cube 1 Answer
Map mouse click state to a Joystick button? 0 Answers
Stop object rotation 1 Answer
"Working with moving and mouse touch to set coordinates" 0 Answers