Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Why are my two bullets shooting simultaneously?

Discussion in '2D' started by wowcrazyguy, Nov 30, 2018.

  1. wowcrazyguy

    wowcrazyguy

    Joined:
    Sep 24, 2018
    Posts:
    49
    Hello everyone,

    I've created a switch weapon system and try to use it to shoot different bullets, but I guess when I added to more than three weapons, I cannot control the bullets. They just shot simultaneously with different rates. Could anyone please have a look about my codes. Thank you

    [Code = CSharp]using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;


    public class playerShoot : MonoBehaviour {

    public GameObject realBullet;
    public GameObject aKbullet;


    public GameObject aK47;
    public GameObject desertEagle;
    public GameObject m16;
    public GameObject pistol;
    public GameObject uzi;
    public GameObject shotGun;
    public GameObject sniper;
    public GameObject rotativeGun;




    public void Attack()
    {

    whichWeapon++;

    if (whichWeapon == 1)
    {

    {
    soundManager.playSound("shootSound");


    GameObject MachineBullet = Instantiate(realBullet, transform.position, Quaternion.identity);

    MachineBullet.GetComponent<fireBullet>().Speed *= transform.localScale.x;

    MachineBullet.GetComponent<fireBullet>().ySpeed = -0.1f;
    MachineBullet.GetComponent<fireBullet>().xSpeed = 0f;

    }

    }
    if (whichWeapon == 2)



    {



    soundManager.playSound("shootSound");



    GameObject aKmachineBullet = Instantiate(aKbullet, transform.position, Quaternion.identity);

    aKmachineBullet.GetComponent<aK47Bullet>().Speed *= transform.localScale.x;

    aKmachineBullet.GetComponent<aK47Bullet>().ySpeed = -0.1f;
    aKmachineBullet.GetComponent<aK47Bullet>().xSpeed = 0f;


    }

    if (whichWeapon == 3) {

    // haven't decided it
    }


    if (whichWeapon == 4)
    {

    // haven't decided it

    }


    if (whichWeapon == 5)
    {

    // haven't decided it

    }


    if (whichWeapon == 6)
    {

    // haven't decided it

    }


    if (whichWeapon == 7)
    {

    // haven't decided it

    }


    if (whichWeapon == 8)
    {

    // haven't decided it

    }



    if (whichWeapon >= 8)
    {
    whichWeapon = 0;



    }


    }





    public void changeWeapon() {


    whichWeapon++;


    if (whichWeapon == 1)
    {

    pistol.gameObject.SetActive(true);
    aK47.gameObject.SetActive(false);
    desertEagle.gameObject.SetActive(false);

    m16.gameObject.SetActive(false);
    uzi.gameObject.SetActive(false);
    shotGun.gameObject.SetActive(false);
    sniper.gameObject.SetActive(false);
    rotativeGun.gameObject.SetActive(false);


    }

    if (whichWeapon == 2)
    {

    aK47.gameObject.SetActive(true);
    desertEagle.gameObject.SetActive(false);

    pistol.gameObject.SetActive(false);
    m16.gameObject.SetActive(false);
    uzi.gameObject.SetActive(false);
    shotGun.gameObject.SetActive(false);
    sniper.gameObject.SetActive(false);
    rotativeGun.gameObject.SetActive(false);


    }

    if (whichWeapon == 3)
    {

    m16.gameObject.SetActive(true);
    desertEagle.gameObject.SetActive(false);
    aK47.gameObject.SetActive(false);
    pistol.gameObject.SetActive(false);
    uzi.gameObject.SetActive(false);
    shotGun.gameObject.SetActive(false);
    sniper.gameObject.SetActive(false);
    rotativeGun.gameObject.SetActive(false);

    }


    if (whichWeapon == 4)
    {

    desertEagle.gameObject.SetActive(true);
    pistol.gameObject.SetActive(false);
    m16.gameObject.SetActive(false);

    aK47.gameObject.SetActive(false);
    uzi.gameObject.SetActive(false);
    shotGun.gameObject.SetActive(false);
    sniper.gameObject.SetActive(false);
    rotativeGun.gameObject.SetActive(false);
    }

    if (whichWeapon == 5)
    {

    uzi.gameObject.SetActive(true);
    pistol.gameObject.SetActive(false);
    m16.gameObject.SetActive(false);
    desertEagle.gameObject.SetActive(false);
    aK47.gameObject.SetActive(false);
    shotGun.gameObject.SetActive(false);
    sniper.gameObject.SetActive(false);
    rotativeGun.gameObject.SetActive(false);

    }


    if (whichWeapon == 6)
    {
    shotGun.gameObject.SetActive(true);
    uzi.gameObject.SetActive(false);
    pistol.gameObject.SetActive(false);
    m16.gameObject.SetActive(false);
    desertEagle.gameObject.SetActive(false);
    aK47.gameObject.SetActive(false);
    sniper.gameObject.SetActive(false);
    rotativeGun.gameObject.SetActive(false);


    }

    if (whichWeapon == 7)
    {

    sniper.gameObject.SetActive(true);
    shotGun.gameObject.SetActive(false);
    uzi.gameObject.SetActive(false);
    pistol.gameObject.SetActive(false);
    m16.gameObject.SetActive(false);
    desertEagle.gameObject.SetActive(false);
    aK47.gameObject.SetActive(false);
    rotativeGun.gameObject.SetActive(false);



    }


    if (whichWeapon == 8)
    {


    rotativeGun.gameObject.SetActive(true);
    sniper.gameObject.SetActive(false);
    shotGun.gameObject.SetActive(false);
    uzi.gameObject.SetActive(false);
    pistol.gameObject.SetActive(false);
    m16.gameObject.SetActive(false);
    desertEagle.gameObject.SetActive(false);
    aK47.gameObject.SetActive(false);




    }



    if (whichWeapon >= 8)
    {
    whichWeapon = 0;



    }



    }
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Should Attack() really increase whichWeapon?
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
  4. wowcrazyguy

    wowcrazyguy

    Joined:
    Sep 24, 2018
    Posts:
    49
    sorry, I forgot have this in my codes:

    public static inc whichWeapon = 0;
     
  5. Thimble2600

    Thimble2600

    Joined:
    Nov 27, 2015
    Posts:
    165
    You might consider using a switch statement instead of having multiple if statements.

    Also you should avoid multiple unneccesary GetComponent calls. In the code below I've changed it so it instantiates the object then gets the fireBullet component then refers to bullet.

    You could also use switch statements in the changeWeapon method to replace all the if statements.

    Code (CSharp):
    1.     public void Attack()
    2.     {
    3.         //whichWeapon++; ?
    4.  
    5.         switch ( whichWeapon )
    6.         {
    7.             case 1:
    8.             soundManager.playSound ( "shootSound" );
    9.             GameObject MachineBullet = Instantiate ( realBullet , transform.position , Quaternion.identity );
    10.  
    11.             fireBulellet bullet = MachineBullet.GetComponent<fireBullet> ( );
    12.             bullet.Speed *= transform.localScale.x;
    13.             bullet.ySpeed = -0.1f;
    14.             bullet.xSpeed = 0f;
    15.             break;
    16.             case 2:
    17.             //...etc
    18.             break;
    19.             //... etc
    20.         }
    21.     }

    Lastly if weapons are incremented or decremented by 1 at a time, surely you'd only need to disable the weapon used in the previous index, ie if using weapon 5 and swapping to weapon 6, only disable weapon 5. If you've implemented going from 1 to 6 by a keyboard press, just make a reference to the last used weapon and disable that weapon by its index.

    No idea why its firing multiple projectiles though, hope you figure it out.
     
  6. wowcrazyguy

    wowcrazyguy

    Joined:
    Sep 24, 2018
    Posts:
    49

    Thank you so much Thimble2600,

    These codes fix all my problems.