- Home /
Pick Up objects when crosshair is over object
Basically I want to know if it's possible to detect if a guitexture is over an object (within a certain distance) and when a button is pressed that object is picked up.
I searched around and found detecting if a mouse is over an object but my cursor isn't always centered so it didn't work for me.
If anyone could point me in the right direction or explain how I'd go about doing this it'd be greatly appreciated!
Answer by aldonaletto · Sep 08, 2013 at 02:26 AM
You could pass the GUITexture position to ViewportPointToRay and use the resulting ray in Raycast. The code below was adapted from the ViewPortPointToRay example, and should be attached to the GUITexture object:
function Update () {
// Get the ray going through the GUI position
var ray : Ray = Camera.main.ViewportPointToRay(transform.position);
// Do a raycast
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
print ("I'm looking at " + hit.transform.name);
else
print ("I'm looking at nothing!");
}
The GUITexture property pixelInset must be centered: its X and Y components must be equal to -Width/2 and -Right/2 (like X=-64, Y=-29, Width=128, Height=58, for instance - this is the default settings when you create a new GUITexture).
NOTE: If the crosshair is always centered in the screen, you can just pass Vector3(0.5, 0.5, 0) to ViewportPointToRay:
var ray : Ray = Camera.main.ViewportPointToRay(Vector3(0.5, 0.5, 0));
I'm at work so I can't test it with my actual game but I'm unsupervised I made a test area and tried it and it works. Thanks :D!
One question though, how would I get the name of what i'm looking at to another script on another object so I'd be able to test it and add the right values to the right variables?
The script above is intended to be attached to the GUITexture, but it can be modified and attached to the object you want like below. It was also modified to do a raycast only when the left mouse button is pressed:
public var guiTex: GUITexture; // drag here the GUITexture from the Hierarchy view
function Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)){ // if left button pressed...
// Get the ray going through the guiTex position
var ray : Ray = Camera.main.ViewportPointToRay(guiTex.transform.position);
// Do a raycast
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)){
// hit.transform.name has the name of the object clicked
// do whatever you want with it
}
}
}
I've been testing the script on my actual game and it works but i can't seem to destroy the object
private var objectToDestroy : GameObject;
public var guiTex: GUITexture; // drag here the GUITexture from the Hierarchy view
function Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)) // if left button pressed...
{
// Get the ray going through the guiTex position
var ray : Ray = Camera.main.ViewportPointToRay(guiTex.transform.position);
// Do a raycast
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
if (hit.transform.name == "rock")
{
objectToDestroy = GameObject.FindGameObjectWithTag("Pickup");
print("$$anonymous$$$$anonymous$$$$anonymous$$ THAT IS A ROC$$anonymous$$");
Destroy (objectToDestroy);
}
}
}
}
I figured I couldn't go Destroy(hit.transform) and I couldn't so I tried a work around but it wouldn't work either. How would I get it to destroy what i'm looking at?
Sorry for so many sub questions but this should be the last one :) Thank you for helping! @aldonaletto
Don't use Find to find the object you've just clicked - the wrong one may be returned, and you already have all the info you need in the hit structure (any reference allows direct access to object properties like gameObject):
public var guiTex: GUITexture; // drag here the GUITexture from the Hierarchy view
function Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)) // if left button pressed...
{
// Get the ray going through the guiTex position
var ray : Ray = Camera.main.ViewportPointToRay(guiTex.transform.position);
// Do a raycast
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
if (hit.transform.name == "rock")
{
Destroy (hit.transform.gameObject); // this is the object you've clicked!
}
}
}
}