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