Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by LeftyTwoGuns · Apr 01, 2014 at 06:05 PM · collisiontageventprojectilesendmessage

Most efficient way to handle projectile collisions?

In regards to bullets, missiles, etc. what is the most performance efficient way to handle their collisions and events?

Is it best to store evens on each object prefab that is effected by projectiles, check the tag of the projectile, and then initiate the event?

Or is it best to store the events on the projectile itself and send them to whatever object it's colliding with?

Comment
Add comment · Show 1
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 nastasache · Apr 02, 2014 at 12:05 PM 1
Share

It's depending by the context and kind of event, but I think it's better and natural to keep the collision control on projectile (as collision action owner) and events on target (as built-in events owner, like 'decrease my health' or 'throw my head on the air'): projectile checking the tag and sending message to the tag owner to execute built-in tag owner events. Also, the targets are persistent and projectiles are volatile: when nobody fire, targets have no reason to look around for collisions.

Regarding performance, see also: http://answers.unity3d.com/questions/678389/networking-raycast-or-gameobject-projectile.html

2 Replies

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

Answer by Gruffy · Apr 02, 2014 at 01:10 PM

For the most general use of projectiles in a game, stick your code on to your projectile as it would be the object that can initiate a change on whatever it is interacting with.

You would for this apply a collider to the object, set it to "isTrigger" and add a rigidbody (here you may choose to make it "isKinematic" or just uncheck the "useGravity" checkbox and contstrain all posiitons and rotations in the later options for the rigidbody component)

Your receiving object should also have a collider attached, though it is not necessary to set to "isTrigger" on that one.

This then sets up a style for coding triggered collisions.

     void OnTriggerEnter(Collider col)
     {
     //all projectile colliding game objects should be tagged "Enemy" or whatever in inspector but that tag must be reflected in the below if conditional
     if(col.gameObject.tag == "Enemy")
     {
       Destroy(col.gameObject);
 //add an explosion or something
 //destroy the projectile that just caused the trigger collision
 Destroy(gameObject);
     }


I have given you a trigger solution due to Triggers being a little more efficient at runtime over OnCollisionEnter() or raycasting(Physics.RayCast)-both options that may otherwise suit you, but are more expensive to use overall). This isnt a quote from Will Goldstone, but it sure stayed in my head form his Unity3X Game Development Essentials book, an excellent early resource.

Cheers bud and hope that helped in some way. take care Gruffy

Comment
Add comment · Show 3 · 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 LeftyTwoGuns · Apr 03, 2014 at 07:59 PM 0
Share

Thank you both for the advice. It all makes sense. I didn't know about the performance difference between Trigger and Collision, so thanks for that as well

avatar image mattkell462 · Feb 21, 2018 at 12:59 AM 0
Share

So the first destroy statement destroys the collider gameobject, but what does the second destroy statement do?

avatar image Gruffy mattkell462 · Feb 21, 2018 at 12:54 PM 0
Share

So the second destroy call is for the actual gameobject handling this code as, in this case we wanted to destroy that object after colliding with hit object. The call to destroy 'col.gameObject is referencing an 'enemy' or 'hittable' object in the game and due to hitting it we remove it...but what about the projectile itself without that call destroy on it? it would continue onwards forever (or until a time limit had passed perhas). Hope that helps bud

avatar image
0

Answer by GianBert · Jul 03, 2017 at 12:31 PM

How do you do an explosion?

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 Gruffy · Jul 04, 2017 at 08:56 AM 0
Share

Hey Gianbert, I would say this is a question in it's self and I would be happy to answer this for you if you pose a new question of the forum about this :) Thanks, its just to keep things tidy and not have a bunch of questions hanging off topical answers type of thing. Cheers bud Edit: in fairness, the question has likely been posed before now, so a quick Unity Answers search term like: "particle explosion after collision" might yield you a good place to start looking into it... or if there is not an answer that quite suits, then roll that question out, we'll be listening :)

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

24 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

Related Questions

create event on collision 2 Answers

Attack Player if Enemy is hit 0 Answers

handling collision with two objects of the same tag 1 Answer

Ignore Collision with Tag Error. 1 Answer

Particle Collision / Trigger not being reported to On_xx Event 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