- Home /
Patrol script won't work.
So I'm literally just trying to have a character patrol through several checkpoints over and over again, but whenever I run the script, the player is moving super fast. No matter what I make the speed, it still goes really fast, so I don't know what's wrong. I've only been using unity for a few weeks so the answer's probably stupid and obvious, but still.
var theEnemy : Transform;
var checkpoints : Transform[];
var currentCheckpoint = 0;
var speed = 0.01;
var currentDistance = Vector3.Distance(checkpoints[currentCheckpoint].position, theEnemy.position);
function Update(){
if(currentCheckpoint < checkpoints.length){
theEnemy.position = Vector3.MoveTowards(checkpoints[currentCheckpoint].position, theEnemy.position, speed * Time.deltaTime);
if(currentDistance<1){
currentCheckpoint++;
}
}
else{
currentCheckpoint = 0;
}
}
Answer by chaosmaker · Nov 07, 2013 at 11:40 PM
Vector3.MoveTowards
static function MoveTowards(current: Vector3, target: Vector3, maxDistanceDelta: float): Vector3; Description Moves a point current in a straight line towards a target point.
Looks like your parameters here are in wrong order:
theEnemy.position = Vector3.MoveTowards(checkpoints[currentCheckpoint].position, theEnemy.position, speed * Time.deltaTime);
Supposed to be like:
theEnemy.position = Vector3.MoveTowards(theEnemy.position, checkpoints[currentCheckpoint].position, speed * Time.deltaTime);
Your answer

Follow this Question
Related Questions
reference an object without pre-assigning. 2 Answers
my object instantiates to much 1 Answer
Stop a enemy by pointing a light at it. 4 Answers
How to remove decimals from Vector3.Distance 3 Answers
Acceleration script 2 Answers