- Home /
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?
Add some details please. If you give us something to work with, like script, we are able to answer your question better.
i want simulate light gun in unity3d.i can get my info(x,y coordinate in pixel) from external device(gun) and every thing is right until this point.
now when i get x,y i want instantiate a projectile in that coordinate and give a force or velocity that projectile goes straight.i did it but my projectile beacause of viewing system(camera) of unity and prespective when goes far it sounds that projectile goes to the center of screen.i do not know how can i add force to projectile that goese realy straight.
in fact i want simulate light gun like old 2d sega games.but my game is 3d.
Answer by duck · Apr 14, 2010 at 02:16 PM
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;
}
thanks for answer.i did it.but there is problem.when i fire in corners of screen the bullet have not a direct path and the bullet goes out of screen.furthermore when the bullet have gravity i have this problem.i got a video.please refer to video to get what i say. http://www.multiupload.com/LAZUICU9G2
so thanks for answer.