- Home /
Can somebody help me and tell me what is wrong with my script on a first person shooter game ?
Hi! I just started to use Unity three months ago and right now I am doing a first person shooter game. I have a major problem with the script which shoots the bullets from the sniper. My script is the following so far :
var Bullet : Rigidbody;
var cannon : Transform;
var initialSpeed = 100.0;
var reloadTime = 0.5;
var ammoCount = 20;
private var lastShot = -10.0;
function Fire () {
if (Time.time > reloadTime + lastShot && ammoCount > 0) {
var instantiatedBullet : Rigidbody = Instantiate(Bullet,cannon.transform.position, cannon.transform.rotation );
instantiatedBullet.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
// Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
ammoCount--;
}
}
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider); is commented out because I want collision between the bullets and the characters.
My problem is that I can not shoot straight forward... my bullets are going 90 degrees to left, so right now I am shooting to my left, not forward, also my bullets are not faced forward , when I shoot them, there are exactly standing, there are vertical, not horizontal, I rotated the prefab, but nothing good happened.
Somebody could help me how to code the bullets for shooting them forward, not to the left, also how to but them in a horizontal position with codes, because rotating or changing the angles of the prefab could not resolve the problem.
I guess your characters axes are messed up. Is the Z axis of your FPS character looking left?
Never the less. We usualy dont actially make bullets in fps games. They are 2 fast to collide anyways. You should use raycast and just display animations/effects on hit.
Hi ! Z-axis is looking forward.
I never used raycast. How I could use that ?
I guess that you imported sniper are facing left in 0 degrees position.
Answer by Hybris · Jan 06, 2013 at 09:23 PM
The problem probably is the local axis of your object which has the script attached. Although this seems weird, I would try to use transform.forward instead of Vector3.forward. So:
instantiatedBullet.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
this would become:
instantiatedBullet.velocity = transform.TransformDirection(transform.forward * initialSpeed));
please note that:
instantiatedBullet.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
is the same as:
instantiatedBullet.velocity = transform.TransformDirection(Vector3.forward * initialSpeed);