Team Size: 1

Role: Programmer

Development Time: A few days

Engine: Unity, C#

About

This is a small project I did in order to learn how to make an AI move in a more realistic manner. The reason for this project is to just increase my knowledge on AI agents in case ill need to impliment one sometime in the future. There are several ways to create an AI movement system but the one ive chosen to follow are the well known Steering behaviors, seek, arrival and flee, persue and wander.

This is the article i used as reference to understanding the different steering behaviors. Understanding Steering Behaviors

Seek

When creating my seek calculation i followed the common steps that could be found in any article regarding steering behaviors.

Coding it myself and adding adjustment that were suitable for my project. This helped me become more familiar with the seek behavior.

Code snippet: Seek
    
Vector3 Seek(Vector3 pos)
{
    desiredVelocity = pos - transform.position;
    float distance = desiredVelocity.magnitude;

    if (distance < slowingRadius)
    {
        desiredVelocity = Vector3.Normalize(desiredVelocity) * (MaxVelocity * (distance / slowingRadius));
    }
    else
    {
        desiredVelocity = Vector3.Normalize(desiredVelocity) * MaxVelocity;
    }
        
    steering = desiredVelocity - velocity;
    steering = Vector3.ClampMagnitude(steering, SeekForce);

    return steering;
}
 

Persue

The AI now goes directly towards the target. However a more realistic AI would try to predict where the target is trying to go. This can effectly be done with the Persue behavior. Instead of moving towards the target the AI will now calculate a prediction of where the target will be in a few seconds and move towrads that position instead.

Code snippet: Persue
 
Vector3 Persue()
{
    Vector3 playerVelocity = player.GetComponent<PlayerMovement>().Velocity;
    
    float distance = Vector3.Distance(target, transform.position);
    float ahead = distance / 10;
    Vector3 futurePos = target + playerVelocity;

    return Seek(futurePos);
}
 

Wandering

Wandering gives the AI an objective when it has nothing to do. This is a common wandering algorithm that i implimented based on the article. I am going to be honest though I wasnt very satisfied with how it performed. While wandering the AI looks wonkey and like its in confusion. Maybe it looks better for flocks, where this wandering calculation is more common. But for a single AI I didnt like how it looked

Code snippet: Wander
 
    Vector3 Wander()
    {
        Vector3 position = transform.position;
        Vector3 direction = transform.right;
        
        Vector3 circleCenter = position + direction * offset;
        displacement = transform.up;
    
        Vector3 v = new Vector3();
        float wanderAngle = Random.Range(0, Mathf.PI * 2);
        v.x = Mathf.Cos(wanderAngle) * wanderCircleRadius;
        v.y = Mathf.Sin(wanderAngle) * wanderCircleRadius;
    
        displacement = circleCenter + v;
    
        return Seek(displacement);
    }