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

How AI shoot trajectory bullet to player?

Discussion in 'Scripting' started by Shiina011, Oct 10, 2020.

  1. Shiina011

    Shiina011

    Joined:
    Oct 1, 2020
    Posts:
    46
    How to make the AI know player position by trajectory path? So it can shoot by those trajectory paths.

    (Or to make it simple, AI already know the player position, the AI just needs to make a trajectory shooting to player)

    I'm kinda lost to make an AI shoot a trajectory bullet to the player. The reference is bowmasters game.
     
  2. Meishin

    Meishin

    Joined:
    Apr 12, 2019
    Posts:
    26
    Heyo, assuming your AI knows your player position (which you'd can just feed him if its a soloplayer, or you'd make a "detection radius", using spherecasting for example, otherwise) then it's just math;

    Let's consider a bullet (going straight), going at BulletSpeed m/s (i.e. it's velocity's magnitude in unity).
    1- Estimate time the bullet will reach destination ;
    Position of the player initially : Player.position(t0)
    Distance between the player and the bullet (~ennemy) : D, in unity Vector3.Distance(Player.position(t0), Bullet.Position)
    If the player is static, your bullet will hit Player in Travel Time = D / BulletSpeed

    2- Estimate where the player will be depending on the distance ;
    If the player moves, he won't be at Player.position(t0) anymore. So where will he be ?
    Most easiest way is assuming the player will continue the movement he had at time t0.
    So given his velocity (3D direction * RunSpeed) Player.Velocity(t0) vector3/s, Player.Position(t1) = Player.Position(t0) + Player.Velocity(t0) * Time

    3- Shoot at estimated position ; Aim (set bullet direction) bullet toward Player.Position(t1) when shooting

    Note that this is a simplified modeled (often used). As you can tell if the player suddenly strafe, the bullet will likely miss (which is often what you want for your AI in shooter's games).
    It is assuming the player's velocity is constant otherwise you have to integrate Player.Position(t1) - Player.Position(t0)=Integrate(vdt)
    It is assuming also the bullet velocity is way higher than the player's, and so the distance between Player and ennemy is constant : Imagine the bullet is very slow, then if the player goes away, the distance will increase and so will the travel time. In contrary if the player approaches, the distance will decrease, and so will the travel time. If so, the 2 equations above are not linear anymore.
     
  3. Shiina011

    Shiina011

    Joined:
    Oct 1, 2020
    Posts:
    46
    Hey thanks for your reply, sorry if I'm not explaining it clearly. What I mean is to make the AI shoot in parabolic to the player position (player not moving).
    I'm searching on google many people use "trajectory" word as parabolic movement, sorry if I'm troubled you by my question.
     
  4. Meishin

    Meishin

    Joined:
    Apr 12, 2019
    Posts:
    26
    Oh well you'll find plenty answers already made for the parabolic behavior of the arrow, so you're asking how to calculate the angle at which the AI should fire the arrow ?
     
  5. Shiina011

    Shiina011

    Joined:
    Oct 1, 2020
    Posts:
    46
    Yes, the angle and how to make the bullet traverse in parabolic. I can't really make it work even after watching many guides.
     
  6. Meishin

    Meishin

    Joined:
    Apr 12, 2019
    Posts:
    26
    Then you have 2 solutions ;
    1st solution is to use Unity Physics ; The arrow has a rigidbody and collider defining mass, drag, angular drag.
    Using that solution requires a custom script on the arrow to "help" it have the right orientation during flight.
    The pros are that you don't have to do much to throw the arrow, just apply an initial force, and then it can reacts in some realistic way.
    The cons are that you'll have to incorporate drag & angular drag coefficients into into angle calculations (not sure how they are defined)

    2nd solution is to attach a moving script on the projectile and simulate your own movement. Either you simulate the velocity of the arrow (2A) and you let unity calculate the position (if you still want to use physics for collision) or you directly set the position of the object (if you real don't want to use physic, like an animation).
    Pros are it's easier to calculate (since you defined the calculation). Cons are its physically biaised.

    Whatever the solution you'll still have to determine the angle at which your AI agent should shoot, by "reversing" the projectile trajectory formula (which depends of the solution you want to take).