Home Unity

advice for Unity bullet trajectory method. How should I do it?

vertex
Offline / Send Message
Pinned
lHiccstriDl vertex
I want to simulate arma 3 like bullet trajectory but I have no idea about how to do it. What should I do? raycast, rigidbody or both? or totally somthing else? I just want to learn about foundations of it. I have a  idea but it sounds like it will destroy performance. Creating and destroying rays constantly to curve it or disabling an objects collider and mesh and adding a raycast to it to detect collision. What should I  do. 

Replies

  • Alex_J
    Offline / Send Message
    Alex_J grand marshal polycounter
    You'll probably have a larger pool of programmers to look at this question if you post it on the official Unity forums.

    There is some bullet physics packages on the asset store for not too expensive that you might check out, just to get ideas. Or, you could check out the Arma modkit. About performance, no need to write anything off as too expensive if you don't already have lots of experience with the subject. Prototype your ideas, then you'll know.
  • poopipe
    Offline / Send Message
    poopipe grand marshal polycounter
    You should be able to plot the flight path without using physics. I'd start by looking up ballistics calculators for real bullets.  There's plenty of info out there for shooters so I'd be very surprised if you couldn't find the sums.

  • lHiccstriDl
    Offline / Send Message
    lHiccstriDl vertex
    I have no idea how to start scripting the bullet how should it work I have no idea I just want to hear the basics how it is done 
  • poopipe
    Offline / Send Message
    poopipe grand marshal polycounter
    Well, the bullet travels forwards, slows down and drops. The rate it drops is constant due to gravity and the rate it slows is related mainly to drag.

    It's pretty straightforward.  It has an initial velocity,  at every tick that's reduced by an amount of drag and then you move it downward by the influence of gravity.  Calculating the correct vector is a simple Google away 

    If you need a specific unity implementation you'll find one on their forums


  • Alex_J
    Offline / Send Message
    Alex_J grand marshal polycounter
    poopipe left out one major factor though. I don't recall the technical term but every bullet has certain flight characteristics that will not only effect when and and at what rate it will drop, but also how much it may rise. So if you buy a box of rifle rounds, oftentimes there will be a chart on the back or the side that will have numbers like +1.6 at 100 yds, +2.1 at 200 yds, -3.6 at 300 yds, and this is telling you where the bullet will strike in relation to your point of aim at the given distance. For instance, the 5.56 bullet the US army is using rises to its highest point at 300 meters, so we zero the rifles so that where you aim at 300 meters is where the bullet is going to strike. You basically are compensating for that couple of inches of bullet rise. This means that at 100 meters, the bullet is going to hit a little lower than the aimpoint. The popular ACOG sight has a reticle that has indications for the standard army way of zeroing -- it has a 100 m and 300 m reticle.

    And, of course, you can get way more complicated than that, but for a "realistic" game, taking account of windage, elevation, and bullet flight path characteristics is more than enough. Actually, I don't think arma has specific flight path characteristics for different calibers, and that is probably a good thing. Even a video game purported to be realistic still has to be a game first, i.e. it has to be fun.  And something that seems random won't be much fun. If you get too realistic, you are getting into things the player will not be able to see or understand and additionally this level of complexity will eat up your programming time, and the only returns will be players thinking the game is to random. 

    Anyway, if you want to get a quick crash course in bullet physics, just google or youtube. I'd make sure its some kind of reputable source though. Lots of gun-nuts out there espousing all sorts of crazy urban legend type of stuff. Once you know enough about that it should be obvious how to implement it into code. Well, enough to make a rough plan for a prototype anyway. If not, that's probably a good indication that you need more experience coding basic things, and for that, if you haven't done the great tutorial game projects in the Unity learn section, I'd start there. Also check out a free tutorial on youtube called Gamer to Game Developer that will step-by-step show you how to build an entire FPS game.


  • Farfarer
    Yeah, you'll be able to build a function that will take in some parameters about the firing position/direction/velocity (maybe add bullet characteristics like different masses/velocities/tolerances and weather conditions like wind once you're happy the simple case works well) and a time since firing and give you out a position.

    By and large it'll be some basic physics calculations (gravity, deceleration).

    Then you're just feeding in the time since firing and taking where it was last frame and where it is this frame and you can raycast between those points.

    I imagine that generally it'll be moving fast enough that a straight line between frames will get you a decent result but you might want to rig it up to do samples at fixed time intervals and raycast each one in turn for a more consistent result.
  • Obscura
    Offline / Send Message
    Obscura grand marshal polycounter
    https://www.youtube.com/watch?v=cix07R1vlhI

    In this video, when they shoot through the walls, it looks like multi-ray cast. The bouncing one could be done with projectiles.
  • lHiccstriDl
    Offline / Send Message
    lHiccstriDl vertex
    I actually have a lot of knowledge on combat etc in real life I was asking for how to do it in unity xd I've managed to make a object that will rotate propperly and will go down by time but its detection is bad I need to cast 2 rays one in previous and one in current position to check detection. How should I  do it? anyone has any idea? And thank for all of your answers it was usefull 
  • Alex_J
    Offline / Send Message
    Alex_J grand marshal polycounter
    It seems you are wanting to look at actual code snippets? Again, this is an artist forum with probably only a handful of experienced programmers. You'll probably find more solutions if you take the question to a more appropriate forum, like the Unity forum or maybe some programmers reddit channels, etc.
  • lHiccstriDl
    Offline / Send Message
    lHiccstriDl vertex
    I already found the way to do it thanks for all of your help 
  • Alex_J
    Offline / Send Message
    Alex_J grand marshal polycounter
    Well, what did you do?
  • lHiccstriDl
    Offline / Send Message
    lHiccstriDl vertex
    bullet object with drag bullet drop windage etc without collider or meash  is the bullet and the collision is checked by 1 ray casted between current and last location of the bullet object 
Sign In or Register to comment.