I know this question may be asked several times, but I couldn’t find a proper solution.
I’m currently developing a FPS game, and want to add a bullet gravity. I’ve considered using a rigidbody bullet, but I wanted to save some performance, so I decided to go with raycast. I’ve heard that it is possible to simulate the gravity effect, but I do not know how.
I’ve thought about putting multiple small pieces of raycast together to simulate the raycast shooting. The problem here is though, it will have some framerate drops and expensive in performance.
Another way that I’ve figured it out is to use a normal raycast(which goes forward), and lower the position depending on the distance once it hitted the object. But problem is that the player can not “predict” the bullet movement and shoot above the target, because the raycast wouldn’t hit anything and return null.
How do you guys simulate gravity to raycast bullet? Any small codes to help me understanding will be great.
This is my solution for a different problem. Not the same because there is no gravity, but it could be applied to your solution:
- My projectiles are fast (around 200m/s) but the distances are bigger (>500m).
- There is no gravity, movement is a straight line. But I cannot just draw a line, the projectile sometimes needs to travel for 2 or 3 seconds to reach the target. That is more than 100 frames at 60fps.
- I don’t use rigidbody because there is no need. There are no physics involved in the projectile.
- I don’t use colliders because the projectile travels very fast. Discrete collisions would fail and continuous collision is overkill. I plan to have hundreds of projectiles on screen at the same time.
- So, my current solution is to do a raycast every frame update. The raycast projects from current projectile position + (projectile speed) * (frame time). That is a mere 2 or 3 meters per frame update.
- If the short raycast hits a collider, the projectile reports the hit. Otherwise, the projectile moves that short distance.
In your case, you can adjust the speed every frame update. Just add (gravity) * (frame time) to the speed.
Hope it helps.
“Im absolute beginner” alert
Interesting question. As I know raycasts, they cant do that; just one straight line… as I know. I see your problem about the “predicting” thing, thats okay. My only simple solution is: try to use standard rigidbodies again. It might be a bit more expensive, but if you navigate around the interface or code, you will probably see something that can help some reduction in performance requirements. You also said that this question is asked several times, i might recommend checking out the internet too, not just UA. You might find a video about it, or anything related. Sorry for no scripts or any better idea, im trying to help. If you managed to find a solution, please make it public, so other people can try it too! Also, if I helped, please tell me, that would be great.
Best regards, Alex
You could always do a series of shorter raycasts, altering each ones rotation slightly.