# Rifle bullets/projectiles in Unity, raycasting or rigidbody?

Hello again gamedevers :) I'm trying to create a fairly simple FPS shooter game. Im using a Rigidbody FPS Controller prefab from Unity. I have a rifle model attached to that, with a further gameobject for the bullet spawn location on the rifle.

I can see in the Editor, that the bulletSpawn object already knows which way it's supposed to be facing (ie. Z axis is pointing correct direction). I've never really used Raycasts before except mobile games to select an object on screen, but my head is telling me that this might be the best way to make realistic bullets.

It suggests to use Raycasts and I understand how to call a ray from the bulletSpawn and set it's direction, and to see what it hits. But what I don't understand is how fast the ray will travel, and how to limit the speed of said ray.

I'm not sure if this is a good way to acheive the gunfire or whether it might be better to create Rigidbodies and addforce() to them.

You guys are always so helpful and often make things MUCH easier to understand that travelling through dozens of old posts and the docs which I cant always follow along with.

How to achieve this effect?

• Achieving realistic bullet ballistics with using only raycast is hard. On the other hand, unity's rigidbodies on high velocities basically go through the objects that is bad for you. But they act pretty realistically and that is good. So I'd recommend you using both, how? You just shoot rigidbody object and have raycast in front of it to handle collisions.
– Nick
Sep 1 '17 at 17:39
• i really like the sound of this, i will give it a go. I'm still worried that the collision is going to happen instantly and am taking a look at Chris' answer below. Thanks for your help Sep 1 '17 at 17:45
• If you shoot rigidbody with a small raycast at the front, collision won't happen instantly, bullet will travel for some time and then hit the target. But as @Chris said, for example in multiplayer game, where there will be lot of bullets flying around, it will affect performance.
– Nick
Sep 1 '17 at 17:47

A simple raycasting would be instantaneous. This is usually referred to as Hitscan. A simple example would be in Doom, some weapons and enemies hit immediately while others can be dodged after they are shot.

However, using physics objects moving at the speed of bullets, you're likely to have trouble when for example a bullet moves so far in a single fixedUpdate frame that it goes from being in front of an object to behind it. There's sometimes no collision event that occurs, or requires a lot more work out of the physics engine.

Also, if you have a lot of bullets flying at once, large numbers of physics objects might put a strain on the game.

If you're looking for more realistic ballistic simulation, like that in Battlefield, you could produce a formula based on weapon type. Essentially you're just creating a line in 3d space with a way to pick a point at a given time. This makes each bullet a simpler set of data, a reference to the formula, a starting time, and starting position.

Edit