- Home /
Bullet collision for space shooter - colliders, rays, etc?
Hello all,
I'm in the process of making a space shooter and need some advice on what to use to detect the collision between the player's bullets and the enemies. I know that rays are commonly used for faster moving projectiles in first person shooters, but would would be the best method for slower moving bullets - triggers, rigidbodies, etc??? I do want the collisions to be as accurate as possible too.
Any code to help get me started would also be fantastic :)
Answer by Paulo-Henrique025 · Aug 07, 2012 at 07:31 PM
Just add rigidbody and them use OnCollisionEnter http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
Thanks Paulo,
I've used Colliders for various things and they do serve the purpose well. I was just wondering what other user's experiences of other methods is. How do rays perform for slower moving projectiles?
Why not just use the colliders? Even if you want to "predict" a collision you can use a big trigger box around the player. Raycast is a expensive technique and I'm sure you can avoid them.
I personnaly use colliders + rigidbodies, works pretty well!
http://www.youtube.com/watch?v=$$anonymous$$FQY$$anonymous$$YUrJsU
Thanks for suggestions.
I've tried both colliders and triggers and the detection works fine, but as the bullets are moving quickly (similar to your game, wondersonic), the collision is being detected at the middle of the object (possibly a frame or 2 later than required).
So, to use triggers/colliders for fast moving bullets, is making the colliders much larger than the object a standard practice? Is this what you did in your game, wondersonic?
Thanks
Exact, for my bullet, I have a larger capsule collider (trigger ON) than my bullet shape. Exactly for the same reasons you are mentioning :)
Also, in order to avoid over allocating objects, I use an ObjectPool$$anonymous$$anager! (in C#)
Answer by Gunslinger3 · Aug 21, 2012 at 08:01 PM
You should use triggers and colliders (normal size). For the bullets rigidbody, turn collision detection to "Continuous dynamic". For the targets rigidbodies set collision detection to "Continuous". Using these settings will lead to longer physics calculations that is more precise with fast moving bullets. Take the good with the bad.
Source: http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-collisionDetectionMode.html
Your answer
Follow this Question
Related Questions
RayCast Problem : 2d 1 Answer
How to stop textures flickering 2 Answers
Optimization of bullet hit calculation per frame 1 Answer
Detect if GameObject is Touching an Imported Level 0 Answers
instantiate decal at raycastHit 1 Answer