Making an AI Opponent Part I

Channel:
Subscribers:
2,700
Published on ● Video Link: https://www.youtube.com/watch?v=HL3X59mxnZg



Duration: 1:28:52
90 views
1


In today's lecture, we're getting towards the end of the basic skills we need to make a video game. Today we roughed out an AI Opponent that someone suggested be a sock goblin, but ended up just being a couple blocks - we can slot in the animations later.


There are a couple important steps to go through:
1) Make a new blueprint that is a character. This has all the stuff built in to make what we want work.
2) Add a Nav Mesh to the world. Scale it out over whatever area you want the AI to be able to pathfind to. Tap P to see green highlight the areas that are navable. By default it should match the settings in the AI controller, so everything *should* (but doesn't always) work automatically. If you ever mess with step size (how tall a step the AI can take) you have to change it in the Nav Mesh settings too.
3) Set up either a timer or other form of limiting how often the AI will Think. I showed a common pattern, which is to save the last time we Think-ed, and to ignore all ticks until it is (last time + delay) seconds now.
4) Once you have Thinking set up, then you get to choose what your AI will do. There's a couple neat options:
A) Do a traceline to see if the player is visible
B) Move towards the player (using the Navmesh)
C) Move towards a random spot (using the Navmesh)
D) See if the player is front of the AI in a cone
E) Do a radius test to see if the player is nearby


To do D and E we used the power of "maths".


To do D, we used a powerful tool called the Dot Product (DP). The DP, if passed two unit vectors (vectors of length 1) will return 1 if they are pointed the same way, 0 if they are orthogonal (at a right angle), and -1 if they are pointed in opposite directions, with other values in the range from -1 to 1 showing you how close the two vectors are to pointing the same way. (It returns the Cosine of the angle of the two vectors).


We use Dot Product to do things like backstabs or what we did here to see if the player is within a cone in front of the AI. We started by just outputting the values from the dot product and working out that values above .65 or so worked for our purposes. We then pathed the AI towards the player if we were in that cone.


To do E we used the Pythagorean Theorem. The distance between two points is the vector length of (the difference between their positions, called a Displacement Vector).


Vector Length and Dot Product are used a *lot* in video games, they're well worth knowing and remembering.







Tags:
is50a
ai
ai controller
navmesh
dot product
vector length
ue4