- Home /
How to click a 3d object in unity3d?
Hi can anyone point me to or tell me what code to use if say I have a cube in my scene and i want it to run an animation when i click it? I know how to do this with gui but not with a actual 3d object in unity like a plane or something? Thanks (:
Dude are you going to accept one of these three good answers???
Accept rate is not about how much he accepts, it is about how much his answers have been accepted. Running after acceptance is useless, I have hundreds of questions where the guy said Thanks but no tick, or simply used the answer and left. At first I could not sleep because of this, waking up sweaty in the middle of the night screa$$anonymous$$g "Tick it will ya!!!!", then I have learnt to live with it.
I wouldn't say accepting is useless. It lets others know that there has been a proper solution given. Huacanacha, +1.
I do not mean accepting is useless, I mean waiting for the guy to accept is most of the time vain. But sure accepting let others know what help to the issue and should be done.
Answer by Huacanacha · Nov 05, 2013 at 04:37 AM
You raycast from the click position (mouse or touch etc), then if you hit an object you want to interact with run whatever code you need.
For starters look at: http://docs.unity3d.com/Documentation/ScriptReference/Input-mousePosition.html http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
You can use a layer mask for Physics.Raycast so you only check the objects you are interested in, or you could use RaycastAll etc.
Answer by clunk47 · Nov 05, 2013 at 04:41 AM
You could use OnMouseDown, or Raycast to do this, be sure to have a collider of some type on the object for either.
OnMouseDown example in C#
Attach this to the object you want detected:
using UnityEngine;
using System.Collections;
public class OnMouseDownExample : MonoBehaviour
{
void OnMouseDown()
{
print (name);
}
}
Raycast example in C#
attach this to any object. Just to make sense, attach to the main camera or an empty.
using UnityEngine;
using System.Collections;
public class RaycastExample : MonoBehaviour
{
Ray ray;
RaycastHit hit;
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
if(Input.GetMouseButtonDown(0))
print(hit.collider.name);
}
}
}
Thanks it works but is there away i could make it so it only works if the object I'm clicking on has a certain tag and I'm using the raycast way but in javaScript???
Very simple.
#pragma strict
var ray : Ray;
var hit : RaycastHit;
function Update ()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit))
{
if(hit.collider.tag == "Enemy")
print(hit.collider.name);
}
}
Thanks quick question what are var ray : Ray; var hit : RaycastHit; for?
Look it up in Unity Script Reference man. Ray is a RAY casting from your $$anonymous$$ouse Position from the $$anonymous$$ain camera in this case. RaycastHit is what the endpoint of the ray is HITTING. Links provided below for more information on Unity Script Reference.
If something answers your question, please accept and vote up. If you have more questions, please ask them as separate questions.
Answer by Eric5h5 · Nov 05, 2013 at 04:41 AM
Use the OnMouseDown function in a script, and attach the script to the object.
Attach it to which object? The one I'm clicking or an empty one with a variable that I can assign what object I want to be affected when clicked??