Predictive Aim in Unity

Поделиться
HTML-код
  • Опубликовано: 19 дек 2024

Комментарии •

  • @TheSeikLuceid
    @TheSeikLuceid 4 года назад +8

    I can vouch. My player has lost it's life many times to this mathematical formula.

  • @cutsceneentertainment6706
    @cutsceneentertainment6706 3 года назад +2

    If my game ever comes out, every innocent player that gets shot will have you to thank XD

  • @podifue
    @podifue 3 года назад +2

    Great tutorial!. It takes great skill to write code like this and run it and it works.

    • @jeangodecoster
      @jeangodecoster  3 года назад +3

      Thanks :-) I did do some preparation work though, but yes I can code for a few hours without testing the code and have it work properly most of the time :-)

  • @stanleymorse3260
    @stanleymorse3260 2 года назад

    Fantastic stuff, I'm 6 days from my deadlines and this saved my ass. This is the only video out there. Translated to 3D nicely too!

    • @jfmods2325
      @jfmods2325 2 года назад

      do you have discord? I'm having trouble transforming into 3d

  • @Faelinor
    @Faelinor 2 года назад

    Thank you so much for this video. It was definitely something I never would have worked out on my own. Your explanation of everything was fantastic too. I was having trouble for a while with it not working, but ended up finding the issue, I had an "a" instead of a "c" in the quadratic equation which messed up the aim ever so slightly.

    • @jeangodecoster
      @jeangodecoster  2 года назад +1

      Always happy to hear back from people who found my video helpful :-) have fun making games

  • @benmacdonald2895
    @benmacdonald2895 2 года назад

    Great video! Using this for my third year university project where I'm implementing AI enemies with different levels of artificial stupidity. At the lowest level of intelligence enemies will just shoot at the direction of the player but at higher levels enemies will use the technique in this video to predictably fire at the player. Many thanks.

  • @NicoLuzquinos
    @NicoLuzquinos 2 месяца назад +1

    Hey, I was just wondering, could you make a video on stopping the turret if there is no firing solution?

  • @lucasmontec
    @lucasmontec Год назад +1

    There seem to be two cases not covered in this video. I had cases with 2 positive roots, where to solve you actually have to take the min, not the max. And two negative roots, for which I haven't solved yet.

    • @jeangodecoster
      @jeangodecoster  Год назад

      Interesting. Yes I didn’t test those cases I suppose it happens when the enemy is moving in the opposite direction or behind the player.
      I’d have to think this through, but I’m considering re-making this video one day and make it a bit better

  • @yersonramirezguerrero7170
    @yersonramirezguerrero7170 3 года назад +1

    You are a genius, make everything easier to see, thank you very much for making this video, it helped me a lot.

  • @omernomer7429
    @omernomer7429 3 года назад +3

    hey, thank you for explaining this! there is little documentation on predictive aiming, but how would you translate it into 3d space? just by using vector3 instead of vector2? also, my "turret" is moving, how could you compensate for this?

    • @jeangodecoster
      @jeangodecoster  3 года назад +1

      If the target is moving in a straight line, you can convert this to a 2D plane in 3D space. I haven’t done this in any of my projects but that’s where I would start. Also, law of cosines is based on Pythagoras which also works in 3D space. Maybe one day I’ll do a 3D version of this tutorial

    • @omernomer7429
      @omernomer7429 3 года назад +1

      @@jeangodecoster couldn't you also just find the target's velocity and subtract the turret's velocity if it is moving? Then use that delta in the equation?

  • @RocketFriday
    @RocketFriday 2 года назад

    5:01
    "it might seem confusing, but that's just to make it easier." xD

  • @kdzmuzo
    @kdzmuzo 29 дней назад

    Thanks for the video. but i'm making a solar system game (for kids) I need to nail lead-targeting for circular orbits. Any help?

    • @jeangodecoster
      @jeangodecoster  8 дней назад

      Hello, that's quite a step up. I don't think I can help with this without having to document myself extensively

  • @thesianglider2894
    @thesianglider2894 2 года назад +1

    Best youtuber ever!!!!!

  • @Trafikanchapo
    @Trafikanchapo 2 года назад

    J'ai été très surpris par ce bout en Français xD. Je m'y attendais pas. En tout cas merci pour ce cours de math. Je vais essayer d'intégrer ca dans mon jeu :D

    • @Trafikanchapo
      @Trafikanchapo 2 года назад

      It's Working ! Thank you again :)

  • @Teh_Yeet
    @Teh_Yeet Год назад

    So good. My logic stopped when you said (dB = sA * (dA / sB)) is the "Same" as (= dA * (sA / sB))... I think if I would have understood that part I would've gotten the rest. But I copied that code, and that worked amazingly. But I really wish I would've understood it better. Thanks for sharing!
    Edit: I get that these 2 equations result in the same outcome, but I don't understand why we chose the other one here. :D

    • @jeangodecoster
      @jeangodecoster  Год назад +1

      Thanks for the feedback.
      The reason I did that was simply to put dA on the left to make it clearer that we can isolate it as a factor.
      I suppose that, in trying to explain how I to the refactoring, I might have made things more confusing :-)

    • @Teh_Yeet
      @Teh_Yeet Год назад

      @@jeangodecoster I see your point! 👍

  • @BucketmanAnimations
    @BucketmanAnimations 2 года назад +1

    Thanks dude! Ur the best!

  • @sadthecat4454
    @sadthecat4454 9 месяцев назад

    Dude, fuck me I spent way too long trying to solve this without your mathematical knowledge. Thanks mate

    • @sadthecat4454
      @sadthecat4454 8 месяцев назад

      update: when you do this in 3D and your discriminant is negative, it doesn't work and there has to be a better solution.

  • @dranzboi
    @dranzboi Год назад

    it does not work, i checked few times, i did as on the video, projectiles always missed

  • @jakesuninspireddevlog2894
    @jakesuninspireddevlog2894 3 года назад

    I feel a bit silly, but i can't figure out where the "1" came from. at 14:26 . how did "dA^2 - r^2*dA^2" turn into " (1-r^2) * dA^2"
    I'm sure I'm just missing some basic simplifying from many years ago aha.

    • @jeangodecoster
      @jeangodecoster  3 года назад

      Let’s replace dA^2 by something simpler to process: X
      That would make it X - X*r^2
      I hope you see how that turns into X * (1-r^2) if you don’t then yeah there’s some catching up with math in order

    • @jakesuninspireddevlog2894
      @jakesuninspireddevlog2894 3 года назад

      @@jeangodecoster I want to thank you very much for this. I've been researching for months on how to get leading aim to work. Your code working on a 3d space game I've been doing. I found articles that showed a formula, or videos with the end result, but the step by step video on the theory is what let me figure out how to calculate the data. (I didn't have rigid body, so I needed to use ,
      Vector3 TargetVelociy()
      {
      //target.transform.rotation = Quaternion.identity;

      Vector3 newPos = target.transform.position; // each frame track the new position
      Vector3 objVelocity = (newPos - prevPos) / Time.deltaTime;
      prevPos = newPos; // update position for next frame calculation
      return objVelocity;
      }
      Not only is it working, but i actually understand.... 75? percent of what is going on here, and more tinkering with it will get me to 99.
      Thank you again!

  • @cutsceneentertainment6706
    @cutsceneentertainment6706 3 года назад

    How would I reference the position of the target's predicted motion (or the Vector3 position of "c" on the triangle) as opposed to the direction? I'd like to try to use this with a LookAt command
    Again awesome tutorial and thanks for your evil training

    • @jeangodecoster
      @jeangodecoster  3 года назад +1

      I need to look back at the video to understand your question :-) will take some time next week

    • @cutsceneentertainment6706
      @cutsceneentertainment6706 3 года назад

      @@jeangodecoster Ah thanks so much and no worries. I was just considering adding an assistive reticle for players to aim at.

    • @jeangodecoster
      @jeangodecoster  3 года назад +1

      @@cutsceneentertainment6706 can you draw me a mock-up of what you want to do?

    • @cutsceneentertainment6706
      @cutsceneentertainment6706 3 года назад

      @@jeangodecoster well if you go to 10:12 in this clip from the game Halo: Reach, you’ll see an example of some space combat:
      ruclips.net/video/qLz7VxdZzGY/видео.html
      You’ll notice the player has two reticles: one in the center of their view, and one that slightly tracks ahead of enemy vessels to show where the player should be aiming if they want to get hits in. I’d like to recreate that second reticle

    • @jeangodecoster
      @jeangodecoster  3 года назад

      @@cutsceneentertainment6706 oh ok so you want to give the player some visual aid to predictively aim at so they can intercept the enemy's trajectory, right?
      I need to look back at my video but iirc we know the direction where we want to shoot and we also know the value of t (time) before projectile impact as well as the projectile's movement speed. So by doing direction.normalized * projectileSpeed you'll get the exact predicted position of impact.

  • @RaidenHeaven
    @RaidenHeaven 2 года назад

    I found this whole I was looking for it. Give you the time for impact and from there you can find the spot.
    From Scott Lembcke - Shooting a Moving Target
    // delta: relative position
    // vr: relative velocity
    // muzzleV: Speed of the bullet (muzzle velocity)
    // returns: Delta time when the projectile will hit, or -1 if impossible
    float AimAhead(Vector3 delta, Vector3 vr, float muzzleV){
    // Quadratic equation coefficients a*t^2 + b*t + c = 0
    float a = Vector3.Dot(vr, vr) - muzzleV*muzzleV;
    float b = 2f*Vector3.Dot(vr, delta);
    float c = Vector3.Dot(delta, delta);
    float desc = b*b - 4f*a*c;
    // If the discriminant is negative, then there is no solution
    if(det > 0f){
    return 2f*c/(Mathf.Sqrt(desc) - b);
    } else {
    return -1f;
    }
    }

    • @jeangodecoster
      @jeangodecoster  2 года назад

      Interesting use case for dot products. thanks for sharing.

    • @jeangodecoster
      @jeangodecoster  2 года назад

      It's a bit weird though that he's doing Dot(vr, vr) and Dot(delta, delta) because the dot product of a vector with itself is simply the square of its magnitude. I wonder if Dot is more performant than calculating the magnitude but I would expect it to be roughly equivalent

    • @RaidenHeaven
      @RaidenHeaven 2 года назад

      @@jeangodecoster Correct! I have no idea why that does it though. But it should work for 3d as well as 2d.

    • @RaidenHeaven
      @RaidenHeaven 2 года назад

      @@jeangodecoster I'm trying to figure out why he is using these a b c.
      I change the code a bit so I can use it with rotation and moving with transform forward. But the AimAhead is the same...

    • @RaidenHeaven
      @RaidenHeaven 2 года назад

      @@jeangodecoster I think you and him do the same thing. He doesn't need to calculate the Cos because the Dot does have a Cos in the method... that's what I read from MathIsFun

  • @vic137
    @vic137 2 года назад

    The world is ruled by math. unfortunately I am bad at it

  • @smawzyv9281
    @smawzyv9281 3 года назад

    Could you do the same for the old blender 2.79 game engine?

  • @physicsstudent451
    @physicsstudent451 2 года назад +1

    Genius it worked :)

  • @FokoPoko991
    @FokoPoko991 2 года назад +1

    i didn't understand a damn thing but listening to all this techno-jargon is entertaining with how absurd it sounds
    love it 10/10

  • @Ozzy89CZ
    @Ozzy89CZ 2 года назад

    And now try it in 3D

    • @jeangodecoster
      @jeangodecoster  2 года назад

      It’s the same principle, and essentially the same math. Not a big deal to do.

    • @jakesuninspireddevlog2894
      @jakesuninspireddevlog2894 2 года назад +1

      @@jeangodecoster I actually used this in my 3d project for a rail shooter. It worked beautifully. If i remember correctly I just had to switch it to vector 3s and it handled everything normally.

    • @jeangodecoster
      @jeangodecoster  2 года назад

      @@jakesuninspireddevlog2894 Awesome, I'm really glad this can help out some people :-)

  • @thecriticizedgamer8508
    @thecriticizedgamer8508 Год назад

    now do acceleration. good luck with that. You're better off using a iterative method.

    • @jeangodecoster
      @jeangodecoster  Год назад

      Acceleration is an interesting challenge, however I see little practical interest for games. It’s just a rethorical problem