Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Artillery ballistics - gravity

Discussion in 'Scripting' started by Scoots, Feb 23, 2010.

  1. Scoots

    Scoots

    Joined:
    Oct 21, 2009
    Posts:
    31
    Hey all, Im trying to design an artillery unit for an RTS game im developing at the moment, however im a bit confused on a few areas regarding unity's physics engine.

    Im trying to launch a projectile through the air from X point too Y at Z distance. Its got to simulate an artilery shot, so its not as simple as firing at a straight line, it needs to arc above in the air and land at its target, which is where my problem arises!

    Im unsure how to calculate the perfect angle and perfect velocity to apply to the projectile in order for the projectile to land exactly on the enemys position:

    So far i know the angle the projectile will be launched at will be clamped between 30 minimum and 80 maximum, the maximum distance is 100 and minimum at 30. What im not sure on is exactly how unitys gravity works, is it every second the projectile will have droped -9.81 units ? or something else?

    I would idealy like to work out the forumla which allows the artillery to aim a perfect shot rather than "cheating myself" and firing the projectile dependant on the distance using pre set veolicitys that i manualy entered.
     
  2. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    gravitational acceleration is an increase in speed per second. So after the first second, the object will be falling at 9.81 m/s, after 2 seconds it will be falling at 19.62 m/s and so on. Note that it doesn't mean the object has fallen either 9.81 meters after one second, or 19.62 meters after two. The actual distance an object has fallen after a given time can be derived by ((acceleration*time)*time)/2. So an object dropped 3 seconds ago will have fallen:
    ((9.81*3)*3)/2 = 44,145 m.

    You could look up on ballistics and trajectories on wikipedia, I'm sure they have tons of ready-to-use formulas. And maybe also some drag physics to simulate the air the body moves through. Good luck...

    EDIT: Here are some useful wiki links:
    Uniform Acceleration

    Trajectories

    Drag

    And finally some very easy to use formulas where all acting forces besides gravity has been cut away

    Trajectory of a projectile
     
  3. Scoots

    Scoots

    Joined:
    Oct 21, 2009
    Posts:
    31
    ah cheers :) thats made it alot clearer now, ill have a read up on this then try apply it ingame.
     
  4. madebynoxc

    madebynoxc

    Joined:
    Jan 14, 2014
    Posts:
    10
    I did the trajectory by using the article from above. Then I just adjusted values to fit the Unity Physics. Here it is:

    Code (CSharp):
    1. this.aimDist = Vector3.Distance(transform.position, endPoint);
    2. this.groundOffset = Mathf.Abs(endPoint.y - transform.position.y) * 1.5f;
    3. this.curAngle = Mathf.Rad2Deg * (Mathf.Asin(Mathf.Min(((this.aimDist + this.groundOffset) * this.gravity)/Mathf.Pow(this.force/100, 2), 1))/2);
    "this.force" is the force applied to the rigidbody, "this.gravity" is gravity of your world (usually 9.81f).
    The distance of shooting depends on force (smth like 10000), angle is adjusted automatically. If the force is not enough, the angle will always be 45. Ground offset is used to shoot correctly at the points that are higher than player. Multiplying by 1.5 is just my adjustment that shows that this function is not perfect. But it works quite good for me
     
  5. GdeCarpentier

    GdeCarpentier

    Joined:
    Jun 18, 2013
    Posts:
    5
    Hi, i just open-sourced a Unity3D project which may be of use. It doesn't use Unity's physics simulation directly, but you do get a great deal of control over the trajectory's arc towards a target position in return. It uses all the stuff mentioned in my Hindawi paper at the bottom of Trajectory of a projectile. It's also extremely fast, completely deterministic, 'pixel-perfect' and frame-rate independent, which might be handy as well. Good luck!
     
    Last edited: Mar 31, 2015