Make AI move using CharacterController.Move instead of rigidbody.velocity?

Hi all!

I am having a big trouble customizing my patrolling AI script to fit into a Pac-Man style stealth game. My current script has parts of a tutorial and parts I wrote myself.

I think it would move better if I used CharacterController.Move, but tell me if there is a better way!

I already tried taking parts of that move script I linked, but somehow I couldn’t get it working properly…

So if you please could point me out what to do, I’m so confused with this :S Here is the script:

var waypoint : Transform[];
var speed : float = 50;
private var currentWaypoint : int;
var loop : boolean = true;

var player : Transform;

function Update () 
{
    if(currentWaypoint < waypoint.length)
    {
	    var target : Vector3 = waypoint[currentWaypoint].position;
	    var moveDirection : Vector3 = target - transform.position;
	    var velocity = rigidbody.velocity;
		    if(moveDirection.magnitude < 1)
		    {
			    currentWaypoint++;
		    }
		    else
		    {
			    velocity = moveDirection.normalized * speed;
		    }
    }
	
    else
    {
	    if(loop)
	    {
		    currentWaypoint=0;
	    }
	    else
	    {
		    velocity = Vector3.zero;
	    }
    }

	    rigidbody.velocity = velocity;
	    transform.LookAt(target);
	
}

function LateUpdate()
{
         //This prevents the enemy from flying upwards
         transform.position.y = 1.7;
}

You’re right: the best way to control characters is… the CharacterController! Moving rigidbodies always have collateral effects. I converted your script to use CharacterControllers instead of rigidbodies. Create or import your character, add the CharacterController using the menu Components->Physics->Character Controller (it will replace the original collider, if any), then add this script to the character.

var waypoint : Transform[];
var speed : float = 50;
private var currentWaypoint : int = 0;
var loop : boolean = true;
var player : Transform;
private var character : CharacterController;

function Start ()
{
    character = GetComponent(CharacterController);
}

function Update () 
{
    if(currentWaypoint < waypoint.length)
    {
        var target : Vector3 = waypoint[currentWaypoint].position;
        target.y = transform.position.y; // keep waypoint at character's height
        var moveDirection : Vector3 = target - transform.position;
        if(moveDirection.magnitude < 1)
        {
            transform.position = target; // force character to waypoint position
            currentWaypoint++;
        }
        else
        {
            transform.LookAt(target);
            character.Move(moveDirection.normalized * speed * Time.deltaTime);
        }
    }
    else
    {
        if(loop)
        {
            currentWaypoint=0;
        }
    }
}

I’ve been wondering how i could make a GameObject follow anything.

I’ve found a solution, well it took along time but i would like to share it with you.

Here is the link https://jsoshsn.makes.org/thimble/LTE4MDAxNDIzMzY=/ai-fcs

Copy this into a JavaScript and watch.

Here is a new updated Script … [171560-charactermovementnew.zip|171560]