- Home /
left hand invaders moving into each other
here is the link http://gapti.com/GameTest/spaceinvader.html
and the enemy moving code.
var enemySpeed : int; var bullet : Rigidbody; static var moveDir = Vector3.right; enum AIState { Swoop, Shoot }
function Start () { state = AIState.Shoot; yield WaitForSeconds(Random.Range(5, 10)); PerformAction(state);
}
function Update () {
var moveIt = enemySpeed * Time.deltaTime;
///make the enemy move side to side
if (transform.position.x >= 6)
{
moveDir = Vector3.left;
}
if (transform.position.x <= -6.0) { moveDir = Vector3.right; } transform.Translate(moveDir * moveIt);
}
function PerformAction( currentState : AIState ) {
switch (currentState) {
case AIState.Swoop:
print("swooping");
break;
case AIState.Shoot:
print("shoot");
var tempBullet: Rigidbody;
tempBullet = Instantiate(bullet, transform.position, transform.rotation);
Start();
break;
// etc...
}
}
$$anonymous$$ight want to tidy up your code a bit, and explain what your question is (Edit your original question). I don't see anything wrong with your game myself.
Answer by duck · Apr 29, 2010 at 07:47 PM
Each frame, your aliens each execute their own Update, one at a time.
What is probably happening, is that when left hand invader hits the edge, it's the last of the group to perform their Updates, so all the others have already moved left a bit further, and now this onem when taking its turn, moves right as a result of the change of direction - on the same frame that the others moved left.
To fix this, you could delay changing direction until every enemy has moved using LateUpdate, like this (irrelevant code removed):
static var moveDir = Vector3.right; static var newMoveDir = Vector3.right;
function Update () {
var moveIt = enemySpeed * Time.deltaTime;
///make the enemy move side to side
if (transform.position.x >= 6)
{
newMoveDir = Vector3.left;
}
if (transform.position.x <= -6.0)
{
newMoveDir = Vector3.right;
}
transform.Translate(moveDir * moveIt);
}
function LateUpdate() { moveDir = newMoveDir; }
So, every alien uses 'moveDir', even if one of the group has indicated that it's time for a change of direction. And then after every Update has completed, LateUpdate gets called, which assigns the new move direction.
no problem - remember to mark the question as 'accepted' by clicking the tick mark.