Raycasting though ViewportPointToRay gives incorrect results

Hi,
I’m using this piece of code to cast a ray though the center of the screen
(including Debug code if I fucked it up somehow)

var hit : RaycastHit;
 var ray = playerCamera.ViewportPointToRay(Vector3(0.5, 0.5, 0));
 if (Physics.Raycast(ray, hit)) {
 print(ray.origin);
 Debug.DrawRay(ray.origin, ray.direction * 100, Color.red, 5.0);
 Debug.Break();

The resulting rays origin seems to be off but direction is correct which results in ray hitting the stuff to the sides and/or below/above what it was actually supposed to hit.

I have absolutely no idea what I’m doing wrong here.

Thanks in advance.

Update: I just found found out that the more my camera is tilted, the more ‘off’ the origin of the ray is.
My camera is a child of a complex character armature and I am modifying the camera’s transform directly.
Could any of those things be affecting ViewportPointToRay?

Just came across the same issue - Turns out the origin of the ray is based on the near clip plane of the camera, so it’s off by at least that amount.

It works just fine for me. Here is my test code:

using UnityEngine;
using System.Collections;

public class ViewportRayTest : MonoBehaviour {

 public Vector3 point = new Vector3(0.5f, 0.5f, 0);

 public void Update() {
 Ray ray = Camera.main.ViewportPointToRay(point);
 Color col = Color.red;
 if(Physics.Raycast(ray)) col = Color.green;
 
 Debug.DrawRay(ray.origin, ray.direction*100, col);
 }
}

Just open a blank scene, add a cube within view of the camera, and drop this script onto the cube. Slide the point around and the ray turns green when it hits the cube, or any other object with a collider.