I have a gun shooting RayCasts. I would like to have 2d bullet textures shooting off from the gun, following the rays, to simulate the bullets being fired. How would i go about doing this?
I have the ray instintate a bullet hole prefab when it collides with a collider, how can i make this not fill up my hierarchy with clones? I have heard about using decals for this prpose, but cannot figure out how to do that.
Thanks!
There are many ways to make bullets follow a ray based on the desired behavior:
- You can use a particle emitter for your gun that fires billboarded particles using the stretched setting in the particle renderer component and then add a world particle collider component to your particle emitter object and set it to send collision messages. This way you can fire off very fast particles (even 1000 mps or better) and the collisions won't miss.
Then when the particles collide with stuff, you can get the particle's collider transform from the OnCollisionEnter handler and spawn your decal for the bullet hole to paste it on the object.
You can of course also make mesh particles and/or also roll your own bullet code, even doing something like a raycast for hit detection and just animate particles moving through space for a visual effect even though you don't put collision on those meshes.
Or you can make meshes do the collision for bullets and turn up the physics timestep in your Edit->Time-> menu settings for the project. (I don't recommend this way because it's expensive on the CPU and you aren't guaranteed to always get a collision. I've still gotten collision misses even with physics checks set to Continuous Dynamic.)
Now, to not fill up your hierarchy view with bullet decal instances, simply create a parent game object in your scene and call it "Bullet Holes". Then every time you instantiate a new bullet hole, make the "Bullet Holes" object the parent to that bullet hole object so in your scene you will only see the parent object unless you expand the view to see the bullet hole decals.
I recommend you just preallocate one fixed bullet hole queue once on game startup and then cycle/reuse the bullet hole queue as new bullets hit things. Make the queue like 100 holes or something and then just keep cycling them. That way you aren't getting GC hitching from continuously creating new ones at runtime.
~hope that helps. gl