Hello,
Trying to work on a project that requires a corsshair in the 2D NGUI View moving over a 3d object,
and detecting it's over that object.
I'm trying to do it with raycasts, but having no luck. If I move the crosshair over the objet and trigger the routine I don't get a hit, but if I move the crosshair further up and over I can get it to show a hit, but oviously on screen the crosshair is not over the object.
Any thoughts? I'll admit Raycasts are not something I'm very familiar with so this could be just a stupid mistake.
Thanks
For reference the script is located on the crosshair UISprite Object, and the only other gameobject is a 3d cube as a test target.
UICamera is the NGUI 2D Camera inside UIRoot.
bool isCrossHairOverTarget() {
Vector3 pos = UICamera.mainCamera.WorldToViewportPoint(transform.localPosition);
Vector3 endpos = Camera.main.ViewportToWorldPoint(pos);
Ray r = Camera.main.ViewportPointToRay(endpos);
RaycastHit rh
= new RaycastHit
();
Debug.DrawRay (r.origin, r.direction * 10, Color.yellow, 5.0f);
if(Physics.Raycast(r, out rh)) {
return true;
}
else {
return false;
}
}