Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am making a small project in Unity, and whenever I walk with the gun and shoot at the same time, the bullets seem to curve and shoot off 2-3 CMs from the center. When I stand still this doesn't happen.

This is my main Javascript code:

@script RequireComponent(AudioSource);

var projectile : Rigidbody;
var speed = 500;
var ammo = 30;
var fireRate = 0.1;
private var nextFire = 0.0;

function Update() {
    if(Input.GetButton ("Fire1") && Time.time > nextFire) {
        if(ammo != 0) {
            nextFire = Time.time + fireRate;
            var clone = Instantiate(projectile, transform.position, transform.root.rotation);
            clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
            ammo = ammo - 1;
        } else {


I assume that these two lines need to be tweaked:

var clone = Instantiate(projectile, transform.position, transform.root.rotation);
clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));

Thanks in advanced, and please remember that I just started Unity, and I might have a difficult time understanding some things. Thanks!


Here is a video -

(If there are any more mistakes in my code, feel free to edit it.)

Thanks in advanced!

share|improve this question
When you say the bullet lands "off-center" do you just mean that they don't land exactly under the crosshair? EG If you move left does the bullet land to the right, if you move right does it land to the left? – Lewis Wakeford Jul 1 '13 at 14:38
When you walk the bullet's movement instead of going to the center of the screen, it shifts or curves so it's a little bit off by like 2-3 cm. I will create a video. Hang tight. – user0000001 Jul 1 '13 at 14:43
Could be the bullets are children of the object doing the shooting. That means they're moving along with the parent. Try removing the parent: transform.parent = null; – Byte56 Jul 1 '13 at 14:53
Added that, but now the spawn point doesn't move with the gun. It just stays on it's original spot, I can walk with my gun and stuff byt the actuall spawn point just stays unattached. I can shoot though. By the way here is a video: – user0000001 Jul 1 '13 at 14:58 – user0000001 Jul 1 '13 at 14:59
up vote 2 down vote accepted

A better way to handle bullets in Unity is have them travel instantaneously through raycasting.

var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit)){
    var bullet = Instantiate(projectile, hit.collider.transform.position, transform.root.rotation);
share|improve this answer
How exactly do I install it in my code? Sorry, if I am asking too much I just have no clue. – user0000001 Jul 2 '13 at 4:33
Replace the two lines you said needed tweaking with it. – tyjkenn Jul 3 '13 at 23:59

The issue is that your shooting with the same speed while moving with different speeds.

Imagine you would throw a stone at an angle of 90 degrees to the rails from a moving train. On the train it looks like the stone flies in a straight line from the train, keeping the 90 degrees.


An observer standing besides the train sees the stone flying in an angle slightly off the 90 degrees.

 O   S

The speed of the train is added to the speed of the flying stone.

This is not what your code is doing. Your bullet always flies at the same speed in the same direction.

The speed of your bullet when firing needs to be its starting speed plus the speed of the player.

This can be done using vectors: Calculate the speed vectors of the player and the bullet and add them to get the correct speed.

You already have the bullet speed vector. So you should be able to do something like this:

var bullet_speed = transform.TransformDirection(Vector3 (0, 0, speed));
clone.velocity=bullet_speed + player.velocity;

This code snippet is not tested, since I don't have Unity.

share|improve this answer
I just tried your code, it says unidentified object Player. – user0000001 Jul 2 '13 at 4:32

the rotation of the projectile might be a little offset from the centre while walking. Try this

var clone = Instantiate(projectile, transform.position, Quaternion.identity);
share|improve this answer
Just tried, and no luck. Same issue is happening. – user0000001 Jul 1 '13 at 8:38
something, that just now I noticed in your code; your velocity component is dependant on a value speed which is always 500. irrespective of the current position and rotation values, it will shoot out a bullet at Vector3(0,0,500). Why not use a transform.TransformDirection(Vector3.forward*magnitude). Play around with different values for the variable magnitude. – Vishnu Jul 1 '13 at 8:44
Ehh, like I said I am total noob, could you provide me with a sample code? – user0000001 Jul 1 '13 at 8:46
By the way, I tried transform.TransformDirection(Vector3.forward*20) and it makes even a bigger curve now. – user0000001 Jul 1 '13 at 8:50
I am going to create a YouTube video, it's very difficult to explain my issue. – user0000001 Jul 1 '13 at 14:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.