Search Unity

Attack from behind?

Discussion in 'Scripting' started by tawdry, Mar 25, 2015.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    How can i get a npc to attack from behind. Not a problem when his victim is facing away but otherwise he just moves straight at him and goes behind whereas i want him to circle round then move in.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Cheat.

    Set an empty GameObject up as a child of the victim. Place it behind the victim. Navigate to that.

    You can also use the dot product to compare facing.
     
  3. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Already tried that bored but as mentioned he goes straight for the point running into or just past the target i'm looking for a way to calculate a semi circle from 2 points and feed co-ords from that path to the npc every few frames.
     
  4. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
  5. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
    It is not trivial to make this work well, i think you need to play with the pathfinding and AI.
    0. separate long-distance pathfinding from "combat range pathfinding" to handle step 3 below.
    1. figure out the threat-zone or "sight zone" of the player which is probably some kind of cone or semicircle that moves with the player, and rotates with the player. (depending on your game type you may be able to use the camera frustum)
    2. increase the cost of any pathfinding "step" that is in this zone so that the enemy npc will avoid walking through this area.(or increase some "threat value" of standing in that location, so the npc will want to move out of sight)
    3. calculate new paths regularly as the player will be moving and turning around.(biggest performance hit probably)

    obviously this is not a one-line unity built in type function.(afaik ;)
     
  6. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    The vec 3 slerp is close to what i was thinking still haven't managed to get it right yet the speed settings are very fickle.