Ray.origin point moving from stationary object?

Hello everyone! I'm having a small problem with ray casting. The game I'm working on is in first person and I've decided to do ray casting from the center of the camera forward a certain range and then see if there is anything in front of me, etc.

That's all working fine.

The original problem occured when I'd do this:

void Update ()
{
    hit = player.mainCamera.ScreenPointToRay( new Vector3( Screen.width/2, Screen.height/2, 0) );

    Debug.DrawRay(hit.origin, hit.direction * weaponRange, Color.yellow);
}

Here I have a ray constantly being updated and a line being draw representing it. But when this runs, the line flickers. At first I thought it was maybe just a graphical glitch due to being called so many times and thought nothing of it until the ray wasn't acting correctly when using `Physics.Raycast(...)`

So upon doing a `Debug.Log( hit.origin )` I noticed that the line was indeed flickering. The y position would flicker from 11.1 which is the y position of my camera and slowly increment down starting from -15. After running it for a while it hit 11.1 and -170.6 flickering back and forth.

As I said above, my game is working fine and the update code was just for seeing the ray line as I was walking around since the actual ray only updates when my `FireWeapon()` function is called and so a gizmo wouldn't work for it.

I just found this odd and wondered if anyway knew why this was happening. Any ideas?

1 Answer

1

I'm wondering if this could be the problem:

Docs: "Resulting ray is in world space, starting on the near plane of the camera" - so the ray does not start on the position of the camera but on the near plane it sounds to me.

Ben