- Home /
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;
}
Answer by aldonaletto · Jul 04, 2011 at 01:54 PM
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;
}
}
}
Thank you sooo much! I have been trying to figure this out for like a week now, but this seemed to be pretty simple. I'll learn from this. Thanks again! :)
Hi! Your script is very very useful, but I have a question regarding how it works with the CharacterController. By using this script to make a 'wander' effect on a AI Player, how can I manage to move the AI Character AND play an animation? $$anonymous$$y AI Character has a 'run' animation and the CharacterController script. Can these two be combined? Thanks!
Sorry, I forgot to mention: For the example I just gave you, I'm using the Third Person Controller that cames in the standar assets with the builted-in animations. I just remove the camera script.
You can change this same script to check the character speed and select the animation according to its actual velocity - like this:
... private var character : CharacterController; private var lastPos : Vector3; // declare this variable
function Start () { character = GetComponent(CharacterController); lastPos = transform.position; // initialize it }
function Update () { // calculate displacement since last frame: var delta = transform.position - lastPos; lastPos = transform.position; // update last position delta.y = 0; // consider only the horizontal displacement // calculate the actual horizontal velocity: var vel = delta.magnitude / Time.deltaTime; if (vel < 0.2) animation.CrossFade("idle"); // lower than 0.2: idle else if (vel < 3.0) animation.CrossFade("walk"); // between 0.2 and 3.0: walk else animation.CrossFade("run"); // higher than 3.0: run // continue original code: if(currentWaypoint < waypoint.length) { ...
Thanks! That helped a lot, I couldn't figure out how to extend the CharacterController properly.
Answer by John-S206 · Jun 25, 2016 at 07:44 PM
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.
Answer by atifatgamesentity · Nov 24, 2020 at 10:05 PM
Here is a new updated Script ... link text
Your answer
![](https://koobas.hobune.stream/wayback/20220612132408im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Should you use CharacterController.Move or SimpleMove for enemies? 1 Answer
Keeping Character From Walking Through Walls 1 Answer
Small Project, tutorials/help needed 0 Answers
Moving AI away or towards player not working correctly. 1 Answer
How to forbid an NPC to move diagonally using Character Controller? 1 Answer