Walker

AI Walker Script

21 Oct 2010
Posted by xeophin

A simple walker script for Unity 3D in C# that tries to avoid obstacles. It has been used in the ProXedural project at art school, and it made the cows walk towards one of the players. To be honest, it does not really deserve the I in AI. But then again, no one expects cows to be very clever.

Maybe someone can use it as a base to produce something better (oh well, who am I kidding, this has already been done).

using UnityEngine;
using System.Collections;
 
/// <summary>
/// A simple AI walking script. Follows a target.
/// </summary>
public class AIWalker : MonoBehaviour
{
 
    internal Vector3 target;
    private CharacterController controller;
    private int counter = 0;
    private Vector3 avoidNormal;
    private Vector3 semiStaticDirection;
 
    private RaycastHit hit;
 
    /// <summary>
    /// The delegate that will execute the current state of the FSM.
    /// </summary>
    private delegate void FState ();
    private FState stateMethod;
 
    /// <summary>
    /// Returns the vector of the current forward motion of the controller.
    /// </summary>
    /// <value>
    /// Returns a vector.
    /// </value>
    private Vector3 forward {
        get { return transform.TransformDirection (Vector3.forward); }
    }
 
    /// <summary>
    /// Helper function to get a right or left vector.
    /// </summary>
    /// <value>
    /// Returns left of right (Vector3).
    /// </value>
    private Vector3 randomLeftOrRight {
        get { return UnityEngine.Random.value > 0.5 ? Vector3.right : Vector3.left; }
    }
 
    /// <summary>
    /// Calculates the direction to the target object.


Navigation



Languages


Syndicate

Syndicate content