Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CoffeeCodes · Aug 01, 2018 at 10:57 PM · collisionphysicsdestroybulletkill

How do you make a bullet disappear upon contact with another object

I'm very new to coding so this is probably a very easy question to answer. I have a sprite who shoots bullets after space is pressed I want them to disappear or be killed after contact with another object in the level. Here is my code for the contact and kill so far:

function OnCollisionEnter(platforms){ Destroy (weapon.bullets); }

This gives me an error saying that 'funtion' is an "unexpected identifier" Please help...

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 Carterryan1990 · Aug 02, 2018 at 12:39 AM

 void OnCollisionEnter(Collision Other)
 { 
 Destroy (this.gameobject);
 }

Comment
Add comment · Show 5 · 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 CoffeeCodes · Aug 10, 2018 at 05:33 PM 0
Share

Thanks! Just to be sure, substitute |gameobject| with whatever I need destroyed correct?

avatar image CoffeeCodes · Aug 10, 2018 at 05:41 PM 0
Share

Gives me an error, unexpected token Other. tried changing Collision Other to what bullet needs to be destroyed upon impact with aka 'platforms'.

avatar image niiicolai CoffeeCodes · Aug 10, 2018 at 05:46 PM 0
Share

Did you add the script to your bullet prefab?

avatar image Carterryan1990 CoffeeCodes · Aug 10, 2018 at 05:51 PM 0
Share

Yes this script should be on the bullet not the platform.

avatar image CoffeeCodes · Aug 12, 2018 at 09:57 PM 0
Share

Thanks guys this helped a lot! =)

avatar image
0

Answer by niiicolai · Aug 10, 2018 at 06:22 PM

Create a new script called Bullet.cs and add it to your bullet prefab. Also ensure that the bullet has a collider.


https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html

Note: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached


 public class Bullet : MonoBehaviour {
 
     void OnCollisionEnter(Collision other) {
         // Collision other => containing information about the other object, this gameobject did hit. 
         // Check https://docs.unity3d.com/ScriptReference/Collision.html

         // Whenever a gameobject with this script enter a valid collision it will destroy itself.
         Destroy(gameObject);
     }    
 }

Depending on your game I would say there can be easier ways to do it. For example if the visual or any physics behaviour like gravity is not needed for the bullet. You could instead use a Raycast in your shooting function like in my example below.


 // the distance of the raycast
 public float fireLength = 10;
 
 // cooldown between bullets in seconds 
 public float fireRate = .1f;
         
 bool isShooting;
 RaycastHit hit;
 
 void Update() {
   var leftClick = Input.GetMouseButtonDown(0);
   if (leftClick && !isShooting) {
     isShooting = true;
     Shoot();
   }
 }
 
 IEnumerator Shoot() {
   // This will create a raycast to the center of our active camera view.
   // If you isn't creating a first person shooter, this isn't exactly what you are
   // looking for, but you can change this by updating 
   // the parameters passed to the Ray below.
   // Check out https://docs.unity3d.com/ScriptReference/Ray-ctor.html 

   Ray ray = Camera.main.ViewportPointToRay(new Vector2(.5f, .5f));            
   if (Physics.Raycast(ray, out hit, fireLength)) {
 
     if(hit.collider.CompareTag("Player")) {
       Debug.Log("We hit a player!");
     }
   }
 
   yield return new WaitForSeconds(fireRate);
   isShooting = false;
 }



In case you are building a 2D game you should look at https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

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.

Follow this Question

Answers Answers and Comments

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

Destroy Bullet on Random Collision 0 Answers

Have bullets be destroyed when they collide with ANY collider 0 Answers

Destroy bullet on random collision 2 Answers

How to destroy bullet on collision! 2 Answers

Object destroys on collision script 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