Unity - Can't Shoot While Moving

Shooting does nothing while moving, but it works perfectly fine while standing still in my Unity project. As far as I can tell no one else has this problem. I just used the Character Controller first person controller. I didn't bother providing the other scripts because they are irrelevant to the problem

Below is my shooting code.

function Update () {
//Shooting
if (Input.GetButton("Fire1")) {
//Fire rate check
if ((Time.time - lastFire) > fireRate && ammo > 0) {
audio.clip = shootSound;
audio.Play();
var mf = Instantiate(muzzleFlash, muzzle.position, muzzle.rotation);
Destroy(mf, flashLength);
//Lose ammo
ammo -= 1;
var hit : RaycastHit;
//if the player hits something
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit)) {
//if they're an opponent
if (hit.collider.tag == "Opponent") {
hit.collider.gameObject.SendMessage("Damage", attack);
}
}
lastFire = Time.time;
}
if ((Time.time - lastFire) > fireRate && ammo <= 0) {
audio.clip = emptySound;
audio.Play();
lastFire = Time.time;
}
}
}


I added Debug.Log after if (Input.GetButton("Fire1")) { and it only got called if I wasn't moving the player. I later made a very short program, the only scripts being:

#pragma strict

function Update () {
if (Input.GetButton("Fire1")) {
Debug.Log("FIRE");
}
}


and

#pragma strict

function Update () {
if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0) {
Debug.Log(Input.GetAxis("Horizontal"));
}
}


The script only printed FIRE when the horizontal axis was 0.

• What have you tried? Have you attached the debugger? Added logging with Debug.Log? Not everyone has access to the other code you reference, and your version may be different. I suggest you brush up on the basic of debugging and try to solve this one on your own. Learning to solve it on your own will increase your skills and allow you to be more productive in the future. – MichaelHouse Nov 8 '14 at 1:30
• I added Debug.Log after if (Input.GetButton("Fire1")) { and it only got called if I wasn't moving the player. I spent a lot of time trying to figure out what the problem was and only came here after exhausting everything I could find. I didn't bother providing the other code I referenced because it was irrelevant to the problem, I made a very short program, the only scripts being #pragma strict function Update () { if (Input.GetButton("Fire1")) { Debug.Log("FIRE"); } } Ran out of characters, continued in next comment. – Leo T. Nov 8 '14 at 1:55
• And #pragma strict function Update () { if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0){ Debug.Log(Input.GetAxis("Horizontal")); }} The debug log only printed FIRE when I was stationary. – Leo T. Nov 8 '14 at 1:57
• Please edit your question to include the details. – MichaelHouse Nov 8 '14 at 1:58
• Alright, thanks, do you have any idea what the problem is – Leo T. Nov 8 '14 at 1:59

I have a sneaking suspicion that your raycast is hitting the player's collider and stopping there. Try offsetting transform.position along the forward vector far enough that it doesn't hit it.

• It doesn't even play the audio which doesn't require the player to hit an opponent. – Leo T. Nov 8 '14 at 14:57

As stated by kprovost7314, the problem was with my computer's mouse settings, to fix it just turn the mouse delay off in mouse settings.