- Home /
Raycast bullet collision problem
So i have been developing this classic game for training where the player is shooting bullets to some weirdly-shaped enemies. Initially, i had the very common problem of bullets going way too fast through some collider which is not triggered when the bullet passes through it (this target is not solid). So i implemented the raycast test which basically creates a raycast between the position of the bullet in the current frame, and the position of bullet in the previous frame, checking for collision hits. This step worked fine, but raycast lead me to another problem: The edges/borders of this target ARE solid (just like a ring or torus, if the bullet collides with the borders then the bullet may ricochet in or out of target area). So, if the bullet ricochets OUT of the target area, the constantly-moving ring may still catch up with the previous position of the bullet when it ricochet. In other words, suppose frame A is exactly when the bullet collides with the ring border at position P, the next frame B the bullet is ricochet out of the ring and not in touch with the borders anymore, meanwhile the ring target area has moved into position P, therefore raycast hit between frames A and B will find a collission.
How can i overcome this problem? Hope i made myself clear, Thank you in advance
Answer by Glurth · Jun 27, 2018 at 03:04 PM
"bullet in the current frame, and the position of bullet in the previous frame"
" the bullet may ricochet "
I believe this combination may be a part of your troubles. Consider; if the bullet hits the border and ricochets at a time that is halfway between two frames. In this case, the line between the bullet position, in each frame will NOT include the target.
I would suggest you leave everything in the "current" frame; Compute the bullets position, assuming it moves in a straight line, at the end of the frame yourself, the formula for that is simply: (position + (velocity*frametime))
Use that as your final point for the raycast, (and current position as raycast start), and check the raycasts against the current position of objects in the scene.
Alternatively, you can COMPUTE the bullets last-frame position, using the same method. (again the advantage to computing over recording it, is you know you are computing a straight line, with no bounce.)
I think i understand, so basically i need to calculate where the bullet is going to be in the next frame and then raycast between that calculated point and current point of the bullet. But what if it ricochets? how do you calculate that.
"how do you calculate that." You just check for an affirmative return when you test your raycast. If you get one, it should ricochet. Oh, are you asking how to compute the position of the bullet after the ricochet? That is tricky when using rigid bodies, better to let the physics system do that itself. Better to adjust the collision mode for the objects to CollisionDetection$$anonymous$$ode.ContinuousDynamic or if necessary, use the rigid body interpolation member (https://docs.unity3d.com/ScriptReference/Rigidbody-interpolation.html). I should have mentioned those first actually, as they may eli$$anonymous$$ate the need for the raycasting at all.
hmmm thanks for the fast response. Like... this is similar to a classic bullet game where the player shoots bullets to weirdly-shaped moving enimies.. I believe it's not quite the resolution that im looking for because i have other factors in the game. Thanks either way!