- Home /
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?
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
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
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
So the first destroy statement destroys the collider gameobject, but what does the second destroy statement do?
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
Answer by GianBert · Jul 03, 2017 at 12:31 PM
How do you do an explosion?
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
Follow this Question
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