Hi, I was having problems for checking if there was an object between the cámera and my player. I finally used this:
ray = camera.ScreenPointToRay(camera.WorldToScreenPoint (tr.position));
Is that correct?
Hi, I was having problems for checking if there was an object between the cámera and my player. I finally used this:
ray = camera.ScreenPointToRay(camera.WorldToScreenPoint (tr.position));
Is that correct?
Depends on what you mean by ‘camera’. If you mean ‘in view of the camera’ then do you mean from the center of the view? If you mean the object holding the camera, then you can construct a Ray between that object’s transform and the player and see if it hits anything before the player.
But in short, if it works, hey, ok.
I found a better solution:
In function Start() I keep the position of the sphere relative to the screen, it’s center!:
midX = Screen.width/2; midY = Screen.height/2; screenPos = Vector3(midX,midY,1);
This way I have one less transform per update.
ray = camera.ScreenPointToRay(screenPos);
Since I always have my camera looking to the sphere, this works great.