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

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.
By the way, since it is pretty hard to understand, I created a video showing the problem.

Here is the video - The World's Best Creator Platform for Online Workplace Learning

Thanks.

EDIT:
I think, like other people said, raycasting is a good method to use. Can some one rewrite my code, so AFTER each collision the “bullet” gets destroyed, because in my code, the bullet can bounce off trees and go very slowly and stuff, but when I destroy it there is no actual impact, the bullet just gets destroyed right when it hits the cube, while it is supposed to make an impact on cube, and then destroy itself.

I took a look at your code. It’s nearly correct, except your should be transforming it by the transform’s forward property. That’s just about the only thing you did wrong. Because of that, I decided to whip up a little example of another way to do what you’re trying to do using c#.

using UnityEngine;
using System.Collections;

public class SpawnProjectile : MonoBehaviour
{
	public GameObject projectile;
	public int speed = 500;
	public int ammo = 30;
	public float fireRate = 0.1f;
	private YieldInstruction yieldTimer;
	
	void Awake()
	{
		yieldTimer = new WaitForSeconds(fireRate);
	}
 
	void Update()
	{
		if (Input.GetButtonDown("Fire1"))
			StartCoroutine("Shoot");
		
		if (Input.GetButtonUp("Fire1"))
			StopCoroutine("Shoot");
	}
	
	IEnumerator Shoot()
	{
		while (ammo > 0)
		{
			GameObject clone = Instantiate(projectile, transform.position, transform.root.rotation) as GameObject;
			clone.rigidbody.velocity = transform.TransformDirection(transform.forward * speed);
			ammo -= 1;
			if (audio)
				audio.Play();
			yield return yieldTimer;
		}
	}
}

Your assumption is right

change
them to

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

and that should do it