1
\$\begingroup\$

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.

I've been reading about, particularly this: http://answers.unity3d.com/questions/22800/raycast-shooting.html

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?

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Nick Sep 1 '17 at 17:39
  • \$\begingroup\$ 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 \$\endgroup\$ – Big T Larrity Sep 1 '17 at 17:45
  • \$\begingroup\$ 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. \$\endgroup\$ – Nick Sep 1 '17 at 17:47
2
\$\begingroup\$

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
Adding a nice tutorial suggested by Nick in the comments.

\$\endgroup\$
  • \$\begingroup\$ Hi Chris, thanks for this answer. I think I understand exactly what you are saying. Basically creating my own raycast formula i would be able to control the rays speed? (and there is no way to do this with current Raycast object??) How do I 'create the line in the 3d space' ? do i just make an object called like MyBulletPath and then move it a set amount of velocity per frame? and how is best in Unity to actually detect what that object hits without it having a rigidbody ? \$\endgroup\$ – Big T Larrity Sep 1 '17 at 17:48
  • \$\begingroup\$ because ideally i dont want to have any bullet objects, id like to just spawn bullethole sprites in the parts of the scenery it hits, and of course detect when another enemy was hit (and ideally where abouts he is hit) \$\endgroup\$ – Big T Larrity Sep 1 '17 at 17:53
  • \$\begingroup\$ @SuperMegaBroBro For reallistic ballistics you use heuns method. You don't actually draw line in 3d space, you simulate it using math. if you still want to draw line in 3d space for some reason, you can just use unity's line renderer. \$\endgroup\$ – Nick Sep 1 '17 at 17:53
  • \$\begingroup\$ Here is good tutorial about unity realistic bullet ballistics: habrador.com/tutorials/unity-realistic-bullets \$\endgroup\$ – Nick Sep 1 '17 at 17:54
  • 1
    \$\begingroup\$ @SuperMegaBroBro Actually it predicts how bullet physics will work, on the second part: habrador.com/tutorials/unity-realistic-bullets/2-targeting It draws trajectory using line renderer, and maths in code. You could use and adapt this math to your project. :) basically yo ucalculate end point of the bullet by this math, and then just make raycast that will be targeted at this point! \$\endgroup\$ – Nick Sep 2 '17 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.