When I shoot from a gun while walking, the bullet is off the center, but when stand still it's fine

Posted by Vlad1k on Game Development See other posts from Game Development or by Vlad1k
Published on 2013-07-01T08:27:52Z Indexed on 2013/07/01 10:30 UTC
Read the original article Hit count: 213

Filed under:

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;
            audio.Play();
        } 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!

© Game Development or respective owner

Related posts about unity