• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by JPB18 · Sep 01, 2012 at 04:28 PM · c#collidersspawningbullets

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JPB18 · Sep 01, 2012 at 04:48 PM 0
Share

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

avatar image JPB18 · Sep 01, 2012 at 05:16 PM 0
Share

For physics, no... For raycasters and camera, yes...

avatar image Fattie · Sep 01, 2012 at 05:19 PM 0
Share

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.

avatar image Fattie · Sep 01, 2012 at 05:22 PM 0
Share

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 naming 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.)

avatar image
0

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
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Spawning off size blocks 1 Answer

A Collider Bug or my negligence? 1 Answer

AI spawning areas. What are the ways to make them? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges