- Home /
detect if i'm looking at specific object
Hi, how to detect if i'm looking at specific object?
i've try this code, but doesn't work.
var ray : Ray;
var hit : RaycastHit;
function Update(){
ray = Camera.main.ScreenPointToRay (Vector3(0,0,0));
if (collider.Raycast (ray, hit, 500.0)) {
print("Hit Object");
} }
Comment
By "looking" at a specific object do you mean actually having the camera look at whatever you clicked on? Or do you just want it to print out something telling you the name of the object? If that is the case, then you just need to change print("Hit Object"); to: print(hit.collider.gameObject.name); or something along the lines of that.
Answer by whydoidoit · Oct 04, 2012 at 07:33 PM
Your screen point is in the bottom left corner of the screen. Either use Screen.width/2 and Screen.height/2 or use viewport coordinates and pass 0.5, 0.5 with this ViewportPointToRay