Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
0
Question by RiverVenom · Mar 04, 2020 at 04:44 PM · collision detectiondestroy objectspace shooterbullets

How do I destroy a bullet when it hits something?

So I am making a space shooter game and I have an issue with the bullets, which is that when they hit an enemy the bullets will just keep going. I want to have it so when the bullets hit an enemy the bullets will be destroyed.

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
1
Best Answer

Answer by tormentoarmagedoom · Mar 04, 2020 at 07:05 PM

Hello.

You really doesnt need to use layermasks.

Just need to check which collider activated the function. Kishotta uses layermasks, but can check for the obkject tag or the object name or anything that fits in your project.

     private void OnCollisionEnter (Collision collision) {
              if (collision.gameObject.tag == "Something")
              {
                  Destroy (gameObject);
              }          

or

   private void OnCollisionEnter (Collision collision) {
   if (collision.gameObject.name.Contains ("something")
      {
          Destroy (gameObject);
      }   

or anything. For example something "maybe useless" but correct, check for the value of a bool variable in a script attached to its parent. (Is just an example to show you you can check for anything)

   private void OnCollisionEnter (Collision collision) {
   if (collision.gameObject.parent.GetCoponent<SomeScript>().someBoolVariable == true)
      {
          Destroy (gameObject);
      }   

And using "this." here is unnecessary. this script should be attached to the bullet object, Because variable "gameObject" (not GameObject) always refear to the object that contains the script.

Comment
Add comment · Show 1 · 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 RiverVenom · Mar 06, 2020 at 01:00 PM 1
Share

Thanks for the help!

avatar image
-1

Answer by Kishotta · Mar 04, 2020 at 05:11 PM

You could attach something like the following to your bullet prefab. You may need to tweak it if you're using 2D colliders. Untested code, but should be close enough.

 [RequireComponent (typeof (Collider))]
 [RequireComponent (typeof (RigidBody))] // Cant remember if Rigidbody is required
 public class DestroyOnCollision : MonoBehaviour {
     public LayerMask CollisionMask;
     
     private void OnCollisionEnter (Collision collision) {
         // if collided object's layer is in layermask
         if (LayerMask == (LayerMask | (1 << collision.gameObject.layer))) {
             Destroy (this.gameObject);
         }            
     }
 }
Comment
Add comment · Show 2 · 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 RiverVenom · Mar 04, 2020 at 05:20 PM 0
Share

I am getting this error message "error CSO119: 'Layer$$anonymous$$ask' is a type, which is not valid in the given context".

avatar image tormentoarmagedoom · Mar 04, 2020 at 07:13 PM 1
Share

Hello @$$anonymous$$ishotta

When helping people, try to evaluate the "level" of the person. If he is asking for how to destroy something, its clear is just starting with Unity, so try to give more simple and easy solution. :D

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.

Follow this Question

Answers Answers and Comments

127 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why are the objects (they are cows) randomly disappearing? 2 Answers

How is it possible to destroy a GameObject twice? 1 Answer

Why can't I destroy this object!? 3 Answers

Fire limit in a Space shooter? 1 Answer

Stopping bullets from outside forcefield 2 Answers


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