In my game I want my player jump without firing their weapon, but when I press the button it jumps and also fires too in same moment.

In this Update method, I used Input.GetButton so my player will fire continuously while holding the button.

void Start () {
    isJumped = true;

// Update is called once per frame
void Update(){
    if (!transform.GetChild (0).gameObject.GetComponent<PlayerHealth> ().hasDied) {

        if (Input.GetButton ("Fire1") && Time.time > firespeed && isJumped == true) {
                nextFire = myTime + firespeed;
                newProjectile = Instantiate (projectile, transform.position, transform.rotation) as GameObject;
                nextFire = nextFire - myTime;
                myTime = 0.0F;
                //  EnergyCharging ();

void JumpButton(){
        if (grounded) {
            grounded = true;
            myRigidBody.AddForce (new Vector2 (0, jumpPower), ForceMode2D.Impulse);
            if(myRigidBody.velocity.y > jumpPower){
                myRigidBody.velocity = new Vector2 (0, jumpPower);
            animator.SetBool ("isGrounded", grounded);
  • \$\begingroup\$ Use a public boolean variable, set it to true when player jumps, check this variable in update method, if it is true, don't fire. \$\endgroup\$
    – azemda
    Commented Sep 16, 2017 at 5:59
  • \$\begingroup\$ Sorry azemda, I don't understand properly. I set it boolean but now what to do i don't get it. \$\endgroup\$ Commented Sep 16, 2017 at 6:08
  • \$\begingroup\$ Declare a boolean variable say, isJumped, in JumpButton() method, set it to false; and in update() method if (Input.GetButton ("Fire1") && Time.time > speed && isJumped == true). Try this \$\endgroup\$
    – azemda
    Commented Sep 16, 2017 at 6:13
  • \$\begingroup\$ Yeah i declare it now \$\endgroup\$ Commented Sep 16, 2017 at 6:14
  • \$\begingroup\$ Sorry azmeda, its not working my player is jump but now my firing mode is not working. \$\endgroup\$ Commented Sep 16, 2017 at 6:26

1 Answer 1


Here is a checklist of things to check:

  1. Does the game use the same button for jumping and attacking ? I don't see the code that looks if the player has pressed the jump button anywhere.

  2. Is "isJumped" turned false at any point by something ? In the code you provided it doesn't. And what's the point of a "isJumped" bool, if the Jump method doesn't change it ?

  3. According to the code you provided, if "isJumped" isn't changed elsewhere, it will always be true, and the player will always be able to attack, even if he is jumping.

P.S. In the "JumpButton()" method you check if grounded is true, and if it is you turn it to true... It already is true because if it weren't, it wouldn't pass the 'if'. So it's useless to assign this bool to true right after the 'if', because it's going to be true anyway.

  • \$\begingroup\$ Re:1) I'm guessing JumpButton is called by an on-screen UI button when it's tapped. A tap also gets registered as a click, and click is mapped to "Fire1" by default. So they're the same input, via a slightly roundabout route. \$\endgroup\$
    – DMGregory
    Commented Sep 16, 2017 at 11:56
  • \$\begingroup\$ Yes, this might be it. But even if it isn't there is still the problem with the "isJumped" bool. The OP's problem is that the character can shoot while he jumps. In his Update() function, in the 'if' he adds as a condition that "isJumped" is true, and he sets it to true in the Start() method, which acts as a constructor of sorts. So if he doesn't change it to false at any other part of the code, it's always going to be true. But even if there is code elsewhere that changes it to false, if firing requires jumping being true, the character will always be able to fire while jumping... \$\endgroup\$
    – Stavros D
    Commented Sep 17, 2017 at 7:12
  • \$\begingroup\$ JumpButton() method not called anywhere rather than this code which i paste it here and its a UI button, so i press anywhere in the screen its jump and fire in same moment. so i just want i press the jump button its only jump. \$\endgroup\$ Commented Sep 18, 2017 at 4:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .