Raycasting to the middle of the screen?

I’ve found out how to make a gun shoot and thats great, but the code won’t shoot directly in the middle of the screen (AKA crosshair). Any help?
EDIT:
I’ve noticed that the effect is played straight from the gun (right of the player). It’s not taking in the rotation, so it won’t shoot where i want too. So anyway to force it to take rotation/shoot to the middle of the screen?

Gun Script:

var shotSound: AudioClip; // drag a shot sound here, if any
var bloodPrefab: GameObject; // drag the blood prefab here, if any
var sparksPrefab: GameObject; // drag the sparks prefab here, if any

function Update(){
   if (Input.GetButtonDown("Fire1")){
      Shoot();
  }
}

function Shoot(){
if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
   var hit: RaycastHit;

if (Physics.Raycast(transform.position, transform.forward, hit)){
    // prepare rotation to instantiate blood or sparks
    var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);

    if (hit.transform.tag == "Enemy"){ // if enemy hit...
        if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot);
           hit.transform.SendMessage("ApplyDamage", 5,             
           SendMessageOptions.DontRequireReceiver); // and consume its health

       } else { // otherwise emit sparks at the hit point
          if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
    }
}

I believe using Camera.ScreenPointToRay will help.

Physics.Raycast(Camera.main.ScreenPointToRay(Screen.width / 2, Screen.height / 2, 0))

Should cast a ray directly to the middle of the screen.

It’s easier to use Camera.ViewportPointToRay. The example on this page exactly shows what you want :wink:

// Unitycript
var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));

if (Physics.Raycast (ray))
{
    // ...
}