0
\$\begingroup\$

So i am trying to create code where by when I shoot, bullets will shoot out of my gun at the same time, from 3 different positions. If I do a singular instantiate, it works fine, like below:

   IEnumerator ShotgunCoolDown()
    {
        PistolGunEffect.SetActive(false);
        canShoot = true;
        yield return new WaitForSeconds(0.45f);
        GetComponent<AudioSource>().PlayOneShot(PistolGunSound);
        canShoot = false;
        StartCoroutine(COPlayOneShot("Fire"));
        RifleGunEffect.GetComponent<ParticleSystem>().Play();
        (Instantiate(PistolBullet, ShotgunPoint.transform.position, transform.root.rotation) as GameObject).GetComponent<BulletScript>().Activate();
        yield return new WaitForSeconds(0.25f);
    }

However if i add multiple instantiates with different positionings, it will spawn the bullet for a millisecond before disappearing:

     IEnumerator ShotgunCoolDown()
        {
            PistolGunEffect.SetActive(false);
            canShoot = true;
            yield return new WaitForSeconds(0.45f);
            GetComponent<AudioSource>().PlayOneShot(PistolGunSound);
            canShoot = false;
            StartCoroutine(COPlayOneShot("Fire"));
            RifleGunEffect.GetComponent<ParticleSystem>().Play();
            (Instantiate(PistolBullet, ShotgunPoint.transform.position, transform.root.rotation) as GameObject).GetComponent<BulletScript>().Activate();
            (Instantiate(PistolBullet, ShotgunPoint1.transform.position, transform.root.rotation) as GameObject).GetComponent<BulletScript>().Activate();
            (Instantiate(PistolBullet, ShotgunPoint2.transform.position, transform.root.rotation) as GameObject).GetComponent<BulletScript>().Activate();             
yield return new WaitForSeconds(0.25f); 
 }

Any ideas how to address this?

\$\endgroup\$
  • 1
    \$\begingroup\$ without any further detail I can just guess that if your bullets have colliders and they are too close they could trigger each other the collision and thus destroy. If that's not the case, I guess could be of help to understand what you BulletScript consists of \$\endgroup\$ – Leggy7 Aug 14 '16 at 22:51
  • \$\begingroup\$ I wish I could comment. Try disabling the colliders for the bullets to see if they don't destroy. How are you handling bullet destroy perhaps they don't need to be destroyed if they collide with another bullet. \$\endgroup\$ – kodaman Aug 14 '16 at 23:17
  • \$\begingroup\$ It might also help to see the physical points where the bullets are instantiated. Maybe a front view of the shotgun, with the three points visible? If you can put a break point in your code directly after instantiating the third bullet, we might even be able to get a freeze-frame of the bullets "firing" from the gun the instant they are created. This would be especially helpful; be sure to select one of the bullets in scene view before screenshotting, as this should make the collider visible. \$\endgroup\$ – Gnemlock Aug 15 '16 at 1:21
0
\$\begingroup\$

It is most probably a problem regarding your bullets being able to collide with bullets. (as noted by the various comments) To solve this, go to Edit -> Project -> Physcis in Unity's menu in the upper left corner. You will see a Layer Collision Matrix. Checking/unchecking boxes enables you to tell the engine which layer can collide with which.

If you have no 'Bullet' layer, create one and then set it up as you wish.

\$\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.