- Home /
Simulating gravity to Raycast bullet
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.
Answer by vyonox · Jun 10, 2017 at 09:05 AM
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.
your solution sounds great, and it makes sense to me, but having a one trouble connecting the raycasts together. As far as I know, the raycast will not report anything unless it hits anything if(Physics.Raycast(...
Can you give me a little example on how you connected multiple raycasts together?
Simple, move the bullet position in each fixed update. The next update, the raycast will start at current bullet position.
This is pseudo-code but easy to understand:
void FixedUpdate()
{
// calculate new velocity (add gravity)
// direction = velocity normalized
// distance = velocity magnitude
if (Physics.Raycast(position, direction, distance))
{
// Hit something
}
else
{
// No hit
position += velocity;
}
}
Your code helped me alot, but I'm having trouble calculating the velocity. Could you give me a quick example?
Answer by Sanyika66 · Jun 10, 2017 at 07:45 AM
"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
Check out these sites, might help you out: http://answers.unity3d.com/questions/349041/simulating-realistic-bullet-physics.html https://www.reddit.com/r/Unity3D/comments/3jerb7/simple_nonphysics_bullet_drop_with_raycasting/ Hope you understand all that chit-chat.
You can not bend the raycast, but you can predict where the raycast should go in the beginning and start the raycast with a little bit of an angle. That way, you can actually implement gravity system.
That's the solution I found so far.
And I seriously do not want to use rigidbodies, because my game is a multiplayer. Implementing gravity with raycast can be very easy, but the way how I do it, can be hard. I'm also trying to add trails to the raycast. $$anonymous$$aybe I did not ask my question clearly. I've checked out several websites you provided, but unexpectly, non of them really helped me. and I did find my solution by my self, and I just need time to check if it works or not. I'll notify in this thread, once i got it working.
Answer by Cynikal · Jun 06, 2017 at 07:26 PM
You could always do a series of shorter raycasts, altering each ones rotation slightly.
The problem in that method is though, the performance isn't good enough... But I would like to know how you would do the series of shorter raycasts. The thing is though, the raycast will go forever till it hits an object. Doesn't it (Even with the range, if the raycast does not detect(hit) anything, it won't do anything will it?)?
Your answer
Follow this Question
Related Questions
raycast not doing good 1 Answer
How to get bullets to hit crosshair 2 Answers
Make my gun model fire bullets 1 Answer
How can i make a raycast in my fps game? 1 Answer
Ray curve for bullet gravity effect, or any way to make it 1 Answer