The Math of Artificial Intelligence in Simulations — Part 1

This is small intro to artificial intelligence in games and simulations. The objective of this experience was to get two little pigs to follow the farmer around a grass plane (low poly of course lol). We will save the fancy graphics for bigger productions.
Cartesian Plane

A Cartesian plane is a two-dimensional coordinate system that uses a grid of perpendicular axes to plot points (X,Y). One of the most important things to remember when working in with AI is that the X and Y axis will always stay at 90 degrees. This will have to do with a lot of calculations performed in 3 dimensions.
Vectors
Vectors are used everywhere in games/simulation design. Vectors can be anywhere in space. Points represent a static position while vectors represent moving from one location to the next. They have a direction and contain a magnitude held in the value of its length (magnitude shows how far it is going). Vectors don’t have a particular location, as they can be located anywhere in the plane. However, they can be added to a starting point.
I used the Pythagorean theorem to find the length of the vector (in this case, it would only be the square root of X squared + Y squared). The length of a vector is represented like this in an equation: ||v||
By getting the direction and length, you can determine whether two vectors are the same.
Move Script

For the pig movement, we need a game object to represent the goal for the pig to aim for, a Vector for the direction it to go in, and a speed value for it to move at a certain rate.
I want the pig to move toward the Farmer. To do this, you want to subtract the goal position minus the pig’s current position. It can be represented in the code like this:
direction = goal.transform.position — transform.position;
The LookAt() method makes the pig turn toward the Farmer and actually look at where it is going.
this.transform.LookAt(goal.transform.position);
Drive Script
My drive script is performing the movement of the farmer in this game. The variables needed were the speed that I wanted the farmer to move and the rotation speed so the farmer could look another way and change direction. I placed all of the code in Update(), since we want the calculations to change/update frame by frame, while the game/simulation is continuing to play.

After grabbing the input for both the vertical and horizontal axis, I multiplied the speed value with the vertical input and the rotation speed with the horizontal value.
Time.deltaTime was multiplied to make sure the game objects move at a consistent speed (normalize the speed).
I want the game object to move according to the world space, and only on the X and Z coordinates, so I added a transform.Translate().
Since we are only want the gameobject(Farmer) to rotate on the Y axis, we have to add a transform.Rotate().
Here’s a small piece of how to create artificial intelligence in games and real world simulations.