- Home /
Is this way of making a raycast 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?
Answer by DaveA · Mar 20, 2012 at 10:47 PM
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.
Thanks. Yes, I refeer to the first, I have the camera following the player and always looking to the player position. Because the player it's always at the center I tryed to pass Vector3(0.5,0.5,1.0) but it doesn't work. I think in my case that would be the best solution(if it worked).
lol..Ok I was thinking in other thing, the Screen goes from 0 to Screen.height and from 0 to Screen.width. Not from 0 to 1...
Answer by troneras · Mar 21, 2012 at 12:07 AM
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.
Your answer
Follow this Question
Related Questions
RaycastHit always returns 0 1 Answer
Raycast hit in OnDrawGizmos but not in Update 1 Answer
Vehicle is vibrating on the edge of the ramp/surface when I get the normal of the surface 0 Answers
RaycastHit.point Collider issue C# 0 Answers
Ray cast affects all colliders in scene if mouse button is not releasesd. 1 Answer