- Home /
left and right movement for ai
Hi, just having some trouble getting my Boolean statements to switch so the character will move left once it has hit 4 on the x axis. It works fine if i do it manually.
This is my script so far:
var enemySpeed: int;
var canMoveR : boolean = false;
var canMoveL : boolean = false;
function Update () {
amtToMove = enemySpeed * Time.deltaTime;
if (transform.position.x == -6)
{ canMoveL= false; canMoveR= true; }
if(transform.position.x >= -6 && canMoveR == true) {
transform.Translate(Vector3.right * amtToMove);
}
if (transform.position.x == 4){
canMoveR = false;
canMoveL= true;
}
if (transform.position.x <= 4 && canMoveL == true ) {
transform.Translate(Vector3.left * amtToMove);
} if (transform.position.x == -6)
{ canMoveL= false; canMoveR= true; }
}
Script still wont switch over after changing the if statements to (transform.position.x <= whatever ). It switches fine on the first if statement at -6. but not for the left movement. Sorry about the bad layout, didn't copy and paste it correctly.
Can you format that a bit better? I'm having trouble understanding some of it.
For starters, ins$$anonymous$$d of checking for
if(transform.position.x == whatever)
you should use
if(transform.position.x >= whatever)
which avoids the chance of overshooting.
EDIT: Or
I've edited my script again, just using the true and false statements to initiate the movement. The if statements aren't switching back and forth should i be using a different type of statement? This is the if statement
if (transform.position.x == -15){
can$$anonymous$$oveL= false;
can$$anonymous$$oveR= true;
}
else if (transform.position.x == -6){
can$$anonymous$$oveR = false;
can$$anonymous$$oveL= true;
}
You need to switch all your == transform conditionals to be >= or
It's just a function update. Should i be using void update? When i use void update i get errors.
Answer by sby666 · Sep 29, 2011 at 06:57 AM
Ah great thanks everyone! I might try using collisions instead of x position and see how that goes.