Search Unity

Making a "Smart Turret" that predicts the location of enemies for better accuracy

Discussion in 'Scripting' started by BigSeal, Jun 15, 2011.

  1. BigSeal

    BigSeal

    Joined:
    Jul 2, 2010
    Posts:
    26
    This type of thing has been in a lot of games, but I can't find examples of how to code it, and I pretty much slept through physics.

    Currently I'm using a turret in fixed position that just looks at the current position of the enemy at all times and fires rockets, but this doesn't work too well unless the enemy is standing still.

    What I'm going for for to have the turret (still in a fixed position) that is able to fire rockets ahead of the enemy so that they will collide if the enemy does not change its speed or direction. The game itself is 2D, so the turret only has to calculate the x and y speed of the target and adjust its rotation based on the speed of the enemy and the speed of its own rockets. Anyone know an equation that will work with this?
     
  2. MarkusDavey

    MarkusDavey

    Joined:
    Jun 3, 2011
    Posts:
    258
    A thought would be to make a script in every turret TARGETABLE object, that contains a vector that the turret can get.

    The vector will be set by the parent script by sending it the vector it is using to move * deltatime * 2 :)

    This wont account for acceleration, but it will be a baseline to work upon :)
     
  3. andorov

    andorov

    Joined:
    Feb 10, 2011
    Posts:
    1,061
    In order to calculate the point of intersection for two moving objects, you need to do some path..

    The path of the bullet is defined as

    b(t) = b + s*d*t

    and the path of the object is defined as

    p(t) = p + v*t

    where

    b is the initial position of the bullet, s is the speed of the bullet, d is the direction you need to solve for and t is time.
    p is the initial position of the object, v is the velocity of the object and t is time.

    The two paths will meet at

    p + v * t = b + s * d * t

    or..

    vt - sdt = b - p

    lets, define b - p as c, because they dont change, so..

    vt - sdt = c

    Now, you have a system of equation, for a 3 dimension vector, it boils down to this

    v_x*t - s*d_x*t = c_x
    v_y*t - s*d_y*t = c_y
    v_z*t - s*d_z*t = c_z

    Since d is a direction, you also know

    1 = d_x^2 + d_y^2 + d_z^2;

    So you have 4 unknowns (d_x, d_y, d_z and t) and 4 equations. Some elementary math later you can solve for t.
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    a maybe a bit simpler but more inacurate approach is to take the distance to the target, calculate the travel time of the rocket from its speed and the distance, and then multiply the movement vector of the target with the traveltime of the rocket to calculate where the target will be when the rocket arrives. its not accurate when the target changes distance during travelling but depending on both speeds it may be a good aproximation. or to get more precise you could iterate this (means take the newly calculated distance) and proceed until the change of the position between two iterations is smaller than your colliders radius so it should hit always.

    you could also make the collider of the target bigger or make the rocket homing so it is always aimed towards the target and follows him.