- Home /
Need advice on non-realistic bullets collision vs distance
Hello!
I need some advice on whether or not doing a distance check between a bullet and an enemy is better performance wise than OnCollisionEnter().
Right now I have my bullets only collide with enemy and wall/platformer layers and they are "destroyed" (or deactivated since I'm pooling) with OnCollisionEnter. Same goes with the enemies receiving the damage code - tied to OnCollisionEnter();
These bullets don't move at super realistic speeds- it's a stylized side scroller so think of contra or megaman speed of bullets.
Alright so would
Update(){
if(distanceFromEnemy < 0.5){
//destroy and damage
}
}
or
OnCollisionEnter(collision : Collision){
//layer check
//destroy and damage
}
be more efficient?
I would like to keep the distFromEnemy in an Update() to make sure there's enough accuracy of the detection. Also if it IS distFromEnemy how would I go about defining distFromEnemy if I have a bunch of enemies on screen? Some sort of array I suppose but how should I check?
The speed difference between a distance check and a collider would favour the distance check and the level of improvement of performance would depend on the types of colliders used. That said physics is pretty fast and unless your enemies are spherical a distance check might yield unrealistic looking hits + if you want the bullets to hit other obstacles it would quickly become unmanageable (and very probably slower).
Thanks guys! The bullets move slow enough to view onscreen. I will eventually test out both methods in empty scenes. What I wanted to do was use OnCollisionEnter() to check collisions with walls and such. (which is what i do now) but perhaps improve the performance of the enemy/bullet collisions by using distance checks ins$$anonymous$$d on OnCollisionEnter(). $$anonymous$$y enemies ARE using a sphere collider as I need them to move up ramps with velocity as there main method of moving - is there a better way?
Here is a video to my game that i'm trying to improve.
Hey thanks :) it's definitely something I've put a lot of work in these past few months. And I'm releasing a huge update Today version 1.5. -
Yes, it's what I'm doing to ignore collisions between unwanted objects. I use it for some raycast operations and generally to avoid registering unnecessary trigger collisions.
gotchya. my only other comment would be it's odd you're having the slightest performance problems, it looks totally easy-going to me - indeed are you sure you HAVE a performace problem? that is a trivial amount of colliders etc. should be AO$$anonymous$$ I would have thought !
Agreed there are very few bullets on screen in the demo!
Answer by Fattie · Sep 24, 2012 at 02:12 PM
Joseph just FWIW
you mention "keeping track of enemies some sort of array"
Here is a novel-length answer
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
funnily enough it actually includes the very typical "closest of pool" idea as an example
wow this is great! thanks for helping me out so much lately!
Your answer
Follow this Question
Related Questions
Collision Detection between certain objects 2 Answers
Optimization of bullet hit calculation per frame 1 Answer
collision wont work 1 Answer
Best practice for OnTriggerEnter detection 1 Answer
Create Bullethole with onCollisionEnter 2 Answers