I am working on a FPS game, and right now im working on bullet ricochets. From what i could find out, bullets rarely ricochet beyond 5 degrees from a hard surface, regardless of angle the bullet is coming in from, and tend to break up into fragments when hitting the hard surface from an angle above 30 degrees.
I already figured out how to limit the angles that a ricochet can happen in, but i dont how to limit the outgoing vector for the direction of the ricochet bullet to 5 degrees from the surface plane.
Vector3 newVelocity=Vector3.Reflect(bulletVelocity,hit.normal);
newVelocity=Vector3.Slerp(Vector3.ProjectOnPlane(bulletVelocity,hit.normal),newVelocity,0.1f); // dampen the reflected direction
Another option is to add a rigidbody to your bullet and let the physics engine bounce the bullet around. You’ll need to use the ‘Continuous’ collision detection mode and add a physics material to the bullet with a small amount of bounce.
I will give the code solution a go, although im not sure im going to stick with it. Theres some things i want to add later and im not sure that this implementation will let me do what i want to do. It is a solution that will work though, for now
That second solution though, I think i tried something similar in the past and the issue i had with it was that the bullets are fast enough that they can completely pass through walls because the distance they cover in a single frame could potentially be a few meters. So now its raycasts, the bullet checks the space inbetween it and where it will be next frame, and then moves to that space if the raycast hits nothing.
I got it figured out. I used ProjectOnPlane and then quaternion.angleaxis to get the axis to rotate the new bullet directions vector on, then used angleaxis again to rotate the bullet, like so
Vector3 NewBulletStartPosition = BulletHitInfo.RaycastHitInfo.point + (BulletHitInfo.RaycastHitInfo.normal * 0.01f);//the point the bullet hit
Vector3 BulletRicochetPlane = Vector3.ProjectOnPlane(BulletHitInfo.BulletHitDirection, BulletHitInfo.RaycastHitInfo.normal);//The direction of the bullet hit, projected on a plane
Vector3 RicochetRotationAxis = Quaternion.AngleAxis(-90f, BulletHitInfo.RaycastHitInfo.normal) * BulletRicochetPlane;//the direction the bullet hit, projected on a plane, rotated 90 degrees
Vector3 NewBulletDirection = Quaternion.AngleAxis(Random.Range(1f, 5f), RicochetRotationAxis) * BulletRicochetPlane;//the new direction of the bullet
CreateNewBulletProjectile(NewBulletStartPosition, NewBulletDirection, BulletSpeed/2f);
Hit info is basically just RaycastHit with some extra stuff lumped in, like the direction and speed the bulet was traveling.
I didnt even know what gaussian random was until this. For ricochets where the bullet stays intact, its already such a small angle that it can ricochet to and bullets that ricochet dont travel very far (in my game, well, project) that i dont think having a normal distribution would change much.
I probably will be adding it at some point for bullet fragmenting though. To have a nice distribution of bullet fragments, with most being where the bullet wouldve ricochet to had it stayed intact. I would also want to make it so the fragments closer to the center of the spread are more damaging due to them being mostly intact. Im basing a lot of what im working on with bullet effects on materials from what i see in this (https://youtu.be/AKhT4QDSqKw?si=_LFgBccDsdNOS2NQ) video, although things will have to be gamified because this is a game.
I do kind of worry if making bullet ricochets would be to expensive performance wise. For single shot weapons its not a big deal but considering full auto weapons, potentially bouncing multiple projectiles, with potentially multiple combatants, it might add up. Ive heard that premature optimization is bad, but i also think that keeping it simple right now wouldnt hurt.
Thanks for the tip, ill be coming back to it later, probably much later. Good sound design btw.
The raycasting is realtive cheap since we only raycast as far as the projectile can travel in a frame/tick. If anything, sound effects and particals is what you should be worried about.
Here is another video showing off distibution, penetration etc