- Home /
 
Bullets spawn behind the craft
Hi there!
So, I've this problem, where I'm trying to fire a bullet from the same location as the player fighter craft. However, the bullet spawns from behind the fighter, and after setting the Colliders on both objects, they collide and eventually, the craft ends up being destroyed by its own bullets.
Here's the function used to fire the bullets (a Coroutine that fires a burst of five bullets):
 IEnumerator gun_fire()
     {
         isFiring = true;
         GunSource.Play();
         for (int x = 0; x < 5; x++)
         {
             Instantiate(bulletPrefab, transform.position, transform.rotation);
             bulletCount -= 1;
             yield return new WaitForSeconds(bulletCool);
         }
 
         isFiring = false;
     }
 
               So, is there any way of preventing this? (it seems that adding some Vector3 coordinates to transform.position doesn't work since they still spawn out of place...
Thanks for any help.
João Borrego
Answer by Fattie · Sep 01, 2012 at 04:43 PM
sure, this is a basic of shooting ! even Hideo Kojima has to do it.
one simple solution is to learn about the neat LAYERS system Unity3D has in their physics. it's great.
it is explained fully in the Unity docs. your bullet layer should only interact with your enemy (or whatever) layer, and >>> NO OTHER <<< layers.
your 'hero' (or whatever) layer will be a different layer, so the bullets will be harmless to the hero.
it's the best and easiest solution.
Yes, but the hero supposedly will not be alone, and will be followed by a squad of similar fighters. As such, I would also like to include "friendly fire" in order to make the player be more careful to its allies movements so they don't fire upon you or vice-versa...
So what you do in that situation is you have another later called "friendly ships". It's very straightforward. Read the doco about how physics LAYERS work, and notice the "hit matrix".
Now, if it is a $$anonymous$$P game there are many other simple solutions. you can set the name or a tag of the bullet as it leaves. in onCollision on your hero, just check the name. if it is one of your own bullets, ignore it.
Another even simpler solution: read my comment on how to use ActualFiringPointLeftGun below.
very simply .................. just move ActualFiringPointLeftGun a very short distance forward so that it does not interact when departing. this can work perfectly, if the hero itself is not moving too quickly.
if the friendly firghters are under your computer control, just make them another layer 'friendly fighters". and use the collision matrix properly under physics settings, read all about it.
if they are actually other players, you'll have to use a na$$anonymous$$g approach.
(Indeed, if you have a limit of say 4 players, which is likely, personally i'd just use a layer for each player. you use a physics layer for everything in Unity3D, don't hold back. basically every concept should have it's own layer, the ground, the buildings, stars, edge markers, whatever.)
Answer by Outlaw Lemur · Sep 01, 2012 at 05:13 PM
Like Fattie said, you need to make sure that the bullets do not effect the player, so use a layer called player that the player uses and have the bullets not effect that, but every other layer. Do this like:
 if (object.layer/*or object.name or object.tag*/ == "Player") return false; //or 0 etc
 
              Your answer