Light gun simulation

Hi,

i want simulate light gun in my game.i have the light gun and is connected to pc and i can get the data(x,y).

now i want fire a projectile in x,y that i got from gun.i can do it but my projectile tent to center of screen beacause of perspective.i want it goes straight.

what must i do?

You need to use Camera.ScreenPointToRay. This gives you a ray whose direction runs from the camera's position in the direction of the screen point specified.

You would then use the ray.direction as your projectile's velocity. Eg - a script placed on the camera might include this function:

var bulletSpeed = 5;

function FireProjectile( screenX, screenY ) {

    var ray = camera.ScreenPointToRay (Vector3(screenX, screenY, 0));
    var bulletRotation = Quaternion.LookRotation(ray.direction);
    var bullet = Instantiate( bulletPrefab, transform.position, bulletRotation);
    bullet.rigidbody.velocity = ray.direction.normalized * bulletSpeed;

}

i test that.i think this a bug!!!