Battlefield Shooting

How does Battlefield do its shooting? I’ve always done Raycast shooting with a linerenderer just drawn between the point hit and the point it came from. But how does Battlefield do it? Do they also use raycast but have delays between when the raycast is sent? Because with your standard Raycasting you can’t do things like bullet drop or travel time, so how does Battlefield do it?

EDIT:
And there’s no way that they use straight up Prefab shooting because the speed of the bullets would be too great and it wouldn’t detect the fact that you hit someone as much as it does

Possible solution #1: Continuous collision detection. Made specifically to prevent tunneling and could allow “straight up prefab shooting”.
Possible solution #2 - raycast between old and new position for a projectile with a speed.

1 Like

What’s continuous collision detection? Care to elaborate? Because if it’s what it sounds like then I’m pretty eager to hear.

EDIT:
Oh I understand, it’s the mode like Continuous Dynamic, Continous, and discrete…

Yep, that. It was made specifically to prevent tunneling.

It’s a pretty easy thing to do actually. You’re thinking of firing a projectile and checking collision in the same frame.

There are two (or more) ways of raycasting a projectile through time:

  • Using simple projectile physics where you can calculate the position of a projectile given its origin, direction and time since start. Using those three variables, you can position the projectile at any point in its trajectory.
    You can, every 0.2 secs, make a Linecast between (Time - start - 0.2) and (Time - start) to check collision through time.

  • Another easy way to do it is to add the direction * force * delta to the projectile every frame and check for a collision in the same direction, but with a small distance (0.5 meters or difference between last and current position).

So does this mean that it’s possible Battlefield is using Prefab-esque shooting?

I’m not sure what you’re asking really :stuck_out_tongue:
They can, and probably use a game object that moves through time that carries the effects, but physics and collisions are probably handled by a physics function and raycasting. At least it’s the most efficient way that i know of for high speed projectiles.

ps: Just to add to my response before, the second solution needs to change it’s direction though time because of gravity.
Both solutions need to update a new position and rotation for the game object acting as a projectile.

The reason I ask is because I’m designing a low-poly battlefield-like game. And I’ve always used Raycasting the problem being that raycasting can’t account for travel time or bullet drop. And if they can then I do not know raycasting well even to program such a thing. So I’m wondering if I should design a prefab-based shooting system or stick to my old raycasting. I don’t want to use raycasting because of the instant travel time and lack of bullet drop, and with Prefab shooting people will have physical bullets that they can see, dodge, etc. (Though they are moving VERY fast it is still possible). But I am also worried that using prefab-based shooting would have collision problems (or) cause a lot of stress to lower-end PC’s (Though all bullets would delete after X amount of time or if they hit something)

That’s why you should use raycasting for high speed projectiles.
You will have to program a projectile system that will make all those calculations with raycasting through time.

Raycasts only check for collisions given an origin and direction, or two points. But using that, you can move the projectile through space and check collisions every frame.

You move the projectile using a direction, and every frame, you check if it hit something between last update and current update with a raycast.

2 Likes

@eskovas My problem in that scenario is it’s simply beyond my ability to understand. The physics system is something I haven’t worked in enough to understand beyond basic Raycasting and collisions. And I have high doubts to any documentation being made on the specific matter we’re talking about.

Honestly, you just need to test it.

Here’s documentation:
https://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetectionMode.html

And here’s a forum discussion:
https://forum.unity3d.com/threads/collision-detection-discrete-vs-continuous-vs-continuous-dynamic.291818/

Sure it can.

This.

Though I typically project ahead. (especially if it is a rocket or laser or something visual) I raycast in front by the speed and length of the projectile to see if it will hit something the next frame. If so, I mark it to hit on the next update and spawn any hit vfx, then on that update move it to the collision and do the damage/vfx. It makes things look a little punchier over doing it after it penetrates. But it really just depends on the look/interaction you are going for.

2 Likes

@zombiegorilla That sort of programming eludes me. I just did a script for Prefab and it is having interesting results. Is there any MAJOR downside to using Continuous Dynamic rigidbody prefabs for bullets rather than raycasting?

Sounds good, I need to double check if I’m already doing that. If not I’ll try it out, thanks.

Battlefield probably has pre-computed bullet trajectories, curved lines converted to polygon segments that they can quickly raycast in stages to work out where a bullet hits.

On a side note is there a way in Unity’s PhysX to calculate a given rigidbody’s trajectory path for ray casting with a given future tracking range/time. As this would enable better realistic bullets, and allow for smart missiles and enemy AI to predict future positions. It would also be handy for golf games to show where the ball will go.

It almost certainly doesn’t. There are infinte number of trajectories. You can’t precompute them all.

Yes, newton’s laws of motion and standard motion formulas.

F = ma
s = v * t + a * t * t /2

etc.

For complex rigid body with joints, you can’t really instantly calculate final position and will have to simulate the rigidbody for required number of steps, or use simplified representation for movement.

3 Likes

Also, for multiplayer games like battlefield, there are custom physics, tricks and deterministic methods used to account for latency to ensure that all players experience the events in the same way.

1 Like

I don’t think there’s such thing as pre-computed bullet trajectories for use in real-time gameplay.
Like @neginfinity said, and like the first solution i said in a previous post, by using standard physics formulas, and knowing the origin, direction and start time, you can calculate every position the projectile will be on.

Yup, that’s why my first solution is good for deterministic projectile physics in multiplayer.
Because of lag, you can account for that difference in the physics formula and perform physics correctly on the server (using position/rotation/animation history to raycast the players).
The thing with correctness, is that it can be very costly and literally almost nobody notices this (except for the own client shooting and the blind server). Battlefield, and many others, fake and simplify so many things that you would be surprised :smile:
For example, sure the server needs to be able to calculate every little thing correctly, but the other players you see on your game don’t need that correctness.
Here are some examples:

  • For shooting and some other things, they use UDP instead of TCP for remote players. If you compare side-by-side two battlefield players, you will see that the other player can miss some shots and a few other things.
  • When they fire bullets, the remote player isn’t precisely aiming for the target. They could be looking down or in another 90º direction. Remote fired bullets also don’t account for lag, because it’s not important for your game, and don’t always actually spawn at the correct position and with correct direction.
  • Although small destruction is somewhat synced between all players, chairs, boxes, etc, that have rigidbodies and can move are not synced. You could be hiding behind something and think you’re safe, but you’re exposed.

There are more things, but i won’t mention everything here :stuck_out_tongue:
There is this video that explains a few networking things that happen in Battlefield:

https://www.youtube.com/watch?v=5OSv5HH1egE

https://www.youtube.com/watch?v=cPr5K23Xk4c

Check that channel’s videos to see how he analyses a lot of things in Battlefield, like dynamic tick-rate, illusions, etc.

3 Likes


Artillery use range tables/calculations to work out where their shot will land.

It seems silly not to pre-calculate a bullet’s basic trajectory in a game about bullets and shells.

You would only need 360 bullet trajectories covering a 90° at 0.25° intervals for each weapon, so a simple array lookup can track a bullet through it’s arc without any calculations.

You would probably want a 60/120/180fps timeline for the projectile for accuracy.

So in theory a 360 [0-90° in 0.25°] x 900 [0-5 second @ 180 fps] array of floats for height and velocity means you can store a single guns table in 2,531 kb.

Raycasting between the points in the trajectory gives you the hit point.

That’s probably just because nobody wants to be trying to solve an equation in the middle of a firefight? However your computer cannot get ‘shell-shocked’ so you don’t have to worry about that.