I’m writing an AI that follows you with Transform.Translate but I can’t figure out how to make it stop moving. Everything I try makes the AI just move in one direction without stopping. ![]()
If you’re moving the object by calling Transform.Translate(), then the way to make it stop moving is to stop calling Transform.Translate().
If that doesn’t answer the question, you’ll probably need to post some code.
I realize that but whatever Vector3 I attempt to use to stop it doesn’t work… ![]()
Don’t call .Translate at all while it’s stopped.
if ( moving ) transform.Translate( movement );
For some reason when my code is outside the movement if it goes the opposite way and doesn’t stop. This is my code, the AI is supposed to follow the player as long as its within 100 distance from it but it craps out when your more than 100 away.
var Player : Transform;
var WalkSpeed = 4;
var LineOfSight = 100;
function Update(){
if((Vector3.Distance(transform.position,Player.position) < LineOfSight) (Vector3.Distance(transform.position,Player.position) > 3)){
transform.LookAt(Player);
LastSeen = Player;
transform.Translate(WalkSpeed*Vector3.forward*Time.deltaTime);
}
}
I worked on it a bit, after it looses the player and goes to where it saw the player last it just craps out and floats off in one way forever.
var Player : Transform;
var WalkSpeed = 4;
var LineOfSight = 10;
private var LastSeen;
function Start(){
LastSeen = transform;
}
function Update(){
if(Vector3.Distance(transform.position,Player.position) > 3){
if((Vector3.Distance(transform.position,Player.position) < LineOfSight)){ //Going to player
transform.LookAt(Player);
LastSeen = Player.position;
transform.Translate(WalkSpeed*Vector3.forward*Time.deltaTime);
}else{//Going to the last place player was seen
transform.LookAt(LastSeen);
transform.Translate(WalkSpeed*Vector3.forward*Time.deltaTime);}
}
}
try not to use Update, things like this are a bit easier to think through logically when you use coroutines… and you will inevitably want to do some pausing and whatnot anyway, and a WaitForSeconds yield is much easier to do than crazy Update() timers.
I also noticed you don’t have any else clauses in there. It looks like what it’s doing is satisfying the conditions of the If, and then when those conditions are no longer satisfied, it’s not giving it anything to go back to. That is probably why it just floats off into oblivion even when it gets too far, it’s not being ever told to stop.
Here’s a basic solution to your problem using a coroutine… In this script the AI will only LookAt the target and translate forward if the distance to the target transform is within or equal to attackRange, but will stop if the distance gets within or equal to stopRange (I think, I didn’t test it):
public var target : Transform;
public var moveSpeed : float = 3.0;
public var attackRange : float = 10.0;
public var stopRange : float = 2.0;
function Start(){
Patrol();
}
function Patrol(){
while(true){
if( AttackRangeCheck() ){ // target object is within range
if( !StopRangeCheck() ){
transform.LookAt(target);
transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
}
}
yield;
}
}
function AttackRangeCheck() : boolean{
var distanceToTarget = Vector3.Distance(transform.position, target.position);
if(distanceToTarget <= attackRange)
return true;
else
return false;
}
function StopRangeCheck() : boolean{
var distanceToTarget = Vector3.Distance(transform.position, target.position);
if(distanceToTarget <= stopRange)
return true;
else
return false;
}