-1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Nov 8 '14 at 1:30
  • \$\begingroup\$ 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. \$\endgroup\$ – Leo T. Nov 8 '14 at 1:55
  • \$\begingroup\$ 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. \$\endgroup\$ – Leo T. Nov 8 '14 at 1:57
  • \$\begingroup\$ Please edit your question to include the details. \$\endgroup\$ – MichaelHouse Nov 8 '14 at 1:58
  • \$\begingroup\$ Alright, thanks, do you have any idea what the problem is \$\endgroup\$ – Leo T. Nov 8 '14 at 1:59
0
\$\begingroup\$

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.

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

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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