I want to check when 3D object (Enemy) is targeted by 2D GUI texture (like crosshair) to for example make it red when i am pointing on an enemy.
A GUITexture has a pixelInset Rect. Make sure that the x and y values are setup as negative 1/2 of the width and height. For example if you use 256 for the width and height of your crosshair, the pixelInset values would be (-128, -128, 256, 256).
With these pixel inset values, the ‘pivot’ will be the center of the texture. GUITextures live in Viewport space, so to Raycast() you would would do:
var pos = Camera.main.ViewportToScreenPoint(transform.position);
var ray = Camera.main.ScreenPointToRay(pos);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit) {
Debug.Log("I hit "+hit.collider.name+", "+hit.collider.tag);
if (hit.collider.tag == "SomeInterestingTag") {
// Do something here
}
}
Note that if you have your crosshair following the mouse, you can use Input.mousePosition and not convert from Viewport to Screen coordinates.
You have to cast a ray from the camera and if it hits an enemy then change the “color” property of your guiTexture, check the documentation of Physics.Raycast