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

			}

		}

	}

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

This is a bit of a waste since you could use

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.

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!!!