Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Simultaneous button press shooting

Discussion in 'Scripting' started by 8r3nd4n, Feb 21, 2013.

  1. 8r3nd4n

    8r3nd4n

    Joined:
    Sep 10, 2011
    Posts:
    30
    Hi all

    I have a game where there are 3 different types of bullets.
    When the player presses (and holds) fire 1, it fires a bullet (left side).
    When the player presses (and holds) fire 2, it fires a bullet (right side).
    When the player presses (and holds) fire 1 and fire 2, it fires a bullet (combined).

    My script sort of works but when firing the combined bullet, it will still fire left side bullet straight after it.

    Anyone can shed some light on this (I'm sure) pretty simple problem:

    Code (csharp):
    1.  
    2. if (Input.GetButton("Fire1")  Time.time > nextFire) {
    3.         if (Input.GetButton("Fire2")  Time.time > nextFire) {
    4.              nextFire = Time.time + fireRate;
    5.             GameObject clone2 = Instantiate(BulletManager.combinedBullet, new Vector3(transform.position.x, transform.position.y +0.7f, transform.position.z), transform.rotation) as GameObject;
    6.         clone2.rigidbody.velocity = transform.TransformDirection (Vector3.up * shotSpeed);
    7.             }
    8.         else
    9.              nextFire = Time.time + fireRate;
    10.              GameObject clone = Instantiate(BulletManager.leftBullet, new Vector3(transform.position.x, transform.position.y +0.7f, transform.position.z), transform.rotation) as GameObject;
    11.         clone.rigidbody.velocity = transform.TransformDirection (Vector3.up * shotSpeed);
    12.            
    13.         }
    14.        
    15.     if (Input.GetButton("Fire2")  Time.time > nextFire) {
    16.          nextFire = Time.time + fireRate;
    17.             GameObject clone = Instantiate(BulletManager.rightBullet, new Vector3(transform.position.x, transform.position.y +0.7f, transform.position.z), transform.rotation) as GameObject;
    18.     clone.rigidbody.velocity = transform.TransformDirection (Vector3.up * shotSpeed);
    19.         }
    20.  
    21.  
    Thanks
     
  2. Raigex

    Raigex

    Joined:
    Jul 6, 2012
    Posts:
    154
    That is because you dont have an option in your code that says if fire 1 and fire 2 pressed do this. You have if fire 1 do if fire 2 do that.
    What you need
    Code (csharp):
    1.  
    2. if((Input.GetButton("Fire1")   Input.GetButton("Fire2") )  Time.time > nextFire)
    3. {
    4.      //Do your code for both
    5. }
    6. else if(Input.getButton("Fire1)  Time.time > nextFire)
    7. {
    8.    //Do code path for left mouse click
    9. }
    10. else if( Input.getButton("Fire2")  Time.time > nextFire)
    11. {
    12.   //do code path for right mouse button
    13. }
    14.  
    15. Your problem is that you check the first and fire, then you check the second and fire you , dont do a check if 1 is pressed fire or if 2 is pressed fire