Making an AI Opponent Part I
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.
Other Videos By Bill Kerney
2021-10-04 | Midterm Review + Equivocation Fallacy |
2021-10-02 | Reading from Files in C++ |
2021-10-02 | Fallacies Part I |
2021-09-30 | How to export and import assets between projects in UE4 |
2021-09-30 | Making an AI Enemy Part II |
2021-09-30 | Invalid/Valid/Sound II + Hamiltonian/Eulerian Paths and Cycles |
2021-09-30 | UE4 Damage System and HUDs with UMG |
2021-09-30 | Top-Down and Bottom-Up Programming |
2021-09-29 | Midterm I Review |
2021-09-29 | Making a Castle with the Procedural Spline Walls System |
2021-09-28 | Making an AI Opponent Part I |
2021-09-28 | Graph Theory I |
2021-09-27 | 2D Vectors and Arrays |
2021-09-27 | Scratch Part II |
2021-09-25 | Auto + Growing Vectors + Nonblocking I/O |
2021-09-24 | Introduction to Scratch |
2021-09-23 | Niagara Part II |
2021-09-23 | Landscape + Foliage, Hitscan Weapons, Niagara Particle Effects |
2021-09-23 | Syllogisms |
2021-09-23 | Vectors vs. Arrays (i.e. why Vectors are better) |
2021-09-22 | Ethics in Autonomous Vehicles |