- Home /
Use raycast to click on object
I am trying to have a raycast on the mouse position. This will allow you to click on an object, the raycast then detects what tag the object is under, then it will do a certain thing. I have tested my script many times but to no avail. I just want someone to look over my function and to see if they see something I am not. I am testing it out by having a quad, with a collider set up with the tag "HealthUpButton", and the raycast on mouse position. Script is below.
var levelUpPoints : int;
var curHealthLevel : int;
function LevelUp() {
var lhit : RaycastHit;
var lray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.GetMouseButtonDown) {
if(Physics.Raycast(lray, lhit, 100)){
if (lhit.transform.tag == "HealthUpButton") {
if(levelUpPoints > 0) {
levelUpPoints --;
curHealthLevel += 1;
}
}
}
}
}
s there a good reason you are limiting the length of the ray?
Id try it with infinity first and get ti working. Then reduce the range if you want
Answer by THECAKEISALIE93 · May 13, 2014 at 01:43 AM
All of you were right, I tried all of your methods and they worked. But in the end I went with a GUI window that can be dragged. Although I did not use all of the methods you supplied for my inventory, I will be implementing them on the interactive computers that will be in my game. Everyone here was a huuuuuge help!!!!
Answer by wibble82 · May 05, 2014 at 06:47 PM
There is any number of reasons your code might not be working - maybe the ray cast isn't hitting the object, or its hitting something but not "HealthUpButton", or perhaps that final if statement isn't being hit.
First step, you need to add some 'Debug.Log' lines in your code. Change it to this:
function LevelUp() {
var lhit : RaycastHit;
var lray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.GetMouseButtonDown) {
Debug.Log("Doing ray test");
if(Physics.Raycast(lray, lhit, 100)){
Debug.Log("Hit something: " + lhit.transform.tag);
if (lhit.transform.tag == "HealthUpButton") {
Debug.Log("Hit the health up button");
if(levelUpPoints > 0) {
Debug.Log("Deducting points");
levelUpPoints --;
curHealthLevel += 1;
}
}
}
}
}
The debug logs will tell you where its getting to, and allow you to diagnose which bit of the code isn't working. In my experience this can be:
Your ray just isn't hitting anything! Try removing the limit to being of length 100 to start with
Your object doesn't have a collider (or the collider is weirdly positioned)
Your object is in the IgnoreRaycast layer
A common gotya - the 'lhit.transform' refers to the transform of the object that contains the rigid body of the collider that was hit! This won't always be the same as the collider that was hit. To get the exact collider use lhit.collider. So you could check lhit.collider.tag
Hope that helps
-Chris
Answer by Jeff-Kesselman · May 05, 2014 at 06:38 PM
This is a bit of a waste since you could use http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseDown.html
But, make sure you have a collider on the object you are casting against and that it isn't in the IgnoreRaycast layer. I'd also start with an infinite length ray and only shorten it once that much is working.
Your answer
Follow this Question
Related Questions
How to destroy specific gameobject with a mouse click once player is looking at them using a Ray 1 Answer
I am stuck and don't know what to do, After upgrading to 2019 mouse and raycast don't work well 1 Answer
question about instantiating 2 Answers
How to create gameobjects by clicking in the scene view in the editor? 1 Answer
Get a raycast to hit only child objects to use axes 1 Answer