No announcement yet.

Can't get AI to properly run away from player.

  • Filter
  • Time
  • Show
Clear All
new posts

    Can't get AI to properly run away from player.

    As the title suggests, I'm stuck. I've tried to figure this out for the past 3 (frustrating)days and I just can't get a target location in the opposite direction of the player from the AI's perspective without strange issues like the AI always running away to the left, or right (not caring where the player is) or it getting to a spot where it's just happy to sit despite the target location being updated or updated to the same location over and over again.

    My AI is very simple and in 2D, I just need the AI to move away from the player when it has focus on the player(My behavior tree is working in that sense, I'm struggling with my behavior tree task that sets the "hiding position" vector that my "MoveDirectlyToward" task uses as a target location. Specifically I need the task to get the direction between the 2 actors, player and AI and set the blackboard value "hiding position" (Used by the movedirectlyto) to some distance away in that direction.

    I know this should be simple and I don't feel particularly proud that after days of googling it I request someone's assistance to basically spell out the process for me but I'm stuck and need help. (I have seen these but I haven't been able to solve this)
    Last edited by Soviet03; 02-10-2017, 05:37 PM.

    I am not sure if it needs to just run away, or run away to a certain location

    You can use "find look at rotation" between the AiChar and PlayerChar, and get the forward vector of that rotation. Then you can find a location in the negative x-direction of that vector which will then be in a line directly away from the player, and use this as the target location.

    If moving to a specific location. You can get the distance to both the Ai and the Player, and if the distance to the Player is greatest, then that must mean that, this location is away from the player.

    Hope you can use the ideas :-)


      You are correct it doesn't have to be a specific location just away from the player, using your advice I have a "find look at rotation" node which is fed from both the player and AI's "get actor location" nodes, it's output is fed into a "get forward vector"- That output is put into a "vector + vector" node where I put -20 for the x axis. Final result is the AI seems to find 1 spot it's happy with and it just sits. I suspect I've misinterpreted your advice. How would I do this "Then you can find a location in the negative x-direction of that vector"? I assume not by using a Vector + Vector with some arbitrary -x value?


        It sounds like you are on the right track. But -20 isn't a lot. It is only 20 cm in game. I think the math should be something like this:

        (AiCharWorldLoc + (ForwardVectorFromLookAtRotation * (-500,-500,0))

        where the 500 is the distance away in cm your searchpoint should be, assuming it is in a horizontal direction only.


          That was helpful and initially the AI runs away now, but if I follow it until it gets to it's point (or back off so that the behavior tree goes to idle behavior in this case returning back to where it spawned) if it sees me again, it flies toward me (past me in the opposite direction). Rinse and repeat, basically every time it sees me it chooses the opposite direction that it should. Click image for larger version

Name:	My Math.png
Views:	1
Size:	311.8 KB
ID:	1122907


            You know what, I posted that comment with the screen shot and I found an obvious problem, I needed the pawn location but accidentally was using the controller location. The math as posted above with the change I just mentioned now works! Thanks so very much for helping me out with this! I seriously appreciate it you made my week.
            Last edited by Soviet03; 02-10-2017, 08:29 PM.