Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Accurate projectile lead aiming with acceleration

Discussion in 'Scripting' started by RobertBleyl, Dec 6, 2020.

  1. RobertBleyl

    RobertBleyl

    Joined:
    May 16, 2019
    Posts:
    6
    Hi,

    I am developing a 3D space shooter where you fire projectiles at moving enemies. I already integrated this into my game: http://wiki.unity3d.com/index.php/Calculating_Lead_For_Projectiles

    But this only works well when there is little to no acceleration, but in my game the enemies fly along curves all the time (and moving along curves obviously change the velocity-vector of RigidBody), leading to missed shots most the time, which is very frustrating to the players.

    I already googled and searched this forum a lot, but I can't find anything useful. Maybe I am bad with searching for this particular problem though ;)
    Is there any Unity code that already exists that I can "plug-in" like the like from above? Or maybe some C or C++ code even that I could translate into C#?
    I heard that the math gets way more complex when having to deal with acceleration - is it even feasible to calculate each frame?

    Any help would be much appreciated!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,753
    You need some way to predict where the target will be in the future.

    One way is to move the target ahead speculatively some amount of time until it is closer than a bullet traveling at a speed would take to each that point, then use that point as your aimpoint.

    If your bullet is not going with constant motion (i.e., slowing from friction and curving from gravity), then you need to simulate the two of them simultaneously and correct to solve for bullet drop, lead, etc. until you find a solution.

    Now you can begin to appreciate why some of the earliest computers were created to calculate artillery shell trajectories, which is precisely this problem.

    You can also appreciate how good the human mind is at calculating this stuff instantly when you watch shotgun skeet shooters consistently blasting clay pigeons!
     
    SparrowGS likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    If you wanna do it for "real" and your projectiles can change velocity on the spot you'll want to aim at (target.position + target.velocity * timeToReachTarget), since you don't have gravity (i assume) you don't need a full on ballistic equation for timeToReachTarget, it can be simply (distanceToTarget * projectile.velocity)
    this is for a place with gravity and a ground plane incase you wonder
    Code (CSharp):
    1.         public static float GetTimeOfFlight(float vel, float angle, float height){
    2.  
    3.             return (2.0f * vel * Mathf.Sin (angle)) / -Physics.gravity.y;
    4.         }

    That said, no reason to actually do all this heavy work, if you do that for every projectile every fixed update i predict a max of 100-200 live projectiles on PC (keeping in mind you need CPU power for other stuff too)

    What I'd do here is calculate a miss-hit chance when firing the bullet and getting an offset either on the target or right next to it(depending if you need to hit or miss) and moving the projectile on a spline or something over there without physics.
    if the projectiles have a significant amount of travel time and you wanna be able to react to bullets you can alter the hit/miss on the fly if the target changes direction or whatever.

    That said, there is something i find really awesome about actually simulating stuff both as a dev and as a player (although as a player you can never really know if it's real or just smoke) but some stuff (space dog fighting might be one) are just too task heavy to do everything.
     
  4. RobertBleyl

    RobertBleyl

    Joined:
    May 16, 2019
    Posts:
    6
    Thanks for your response. The link I posted does what you mentioned first, but the "timeToReachTarget" calculation is the problem, it's not exact enough.

    I don't think I need ballistics calculation since I don't have gravity in my game, and my projectiles travel at a constant speed. Only the targets can change direction and their velocity.

    Simulating multiple frames ahead does not seem feasibly at all. Especially with the way I move my space ships: it is physics based where each ship has it's own PIDController so you can turn your ships as if you where in an atmosphere (not realistic since space has no air so no friction, but it's more fun that way ^^). I don't see that this kind of calculation should be done for multiple frames in each frame...
     
  5. RobertBleyl

    RobertBleyl

    Joined:
    May 16, 2019
    Posts:
    6
    Thanks for your response. You are right, I don't think I need ballistics like that. I don't have gravity and my bullets do in fact travel at a constant speed. But nothing you wrote really helps me here (see my other response above). :(
     
  6. HellGate94

    HellGate94

    Joined:
    Sep 21, 2017
    Posts:
    132
    Last edited: Dec 7, 2020
  7. jadvrodrigues

    jadvrodrigues

    Joined:
    Feb 25, 2020
    Posts:
    11
    For anyone interested, I created a project that implements that same equation efficiently. You can find it here.
     
    mgear likes this.