Raycast problem

I have copied some parts of the code from the FPS Tutorial. The script that I have problem is the machine gun script. In my script I’ve declared a raycast and it follows the direction that I’ve defined. And here if I start to shoot, the start position of the ray is not centered(a bit off from the center, I can only move my gun’s position to fix it, which I don’t wish to).

Here’s the code:

var direction = transform.TransformDirection(Vector3.forward);

and so I’ve tried to define as the following:

var direction = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0)); 

But unity doesn’t allow me to define like this here.
Can someone please help me to correct the ray’s direction?
Thanks for any help!

ViewportPointToRay does not return a ‘direction’ - it returns a ray that you then use to ray cast.

var ray = Camera.main.ViewportPointToRay(new Vector3(0.5, 0.5, 0.0));
if(Physics.Raycast(ray, Mathf.Infinity)){
     Debug.Log("Hit!");
}