How I handled moving ships left and right (Lecture 110 )

Basically I stored the Vector3 as a variable and used that to determine whether the enemy ships should move left or right in the EnemySpawner script. Below is the code on how I handled it. I’m still going through the lecture, but I thought I would share how I did it.

//Start movement in the left direction.
private Vector3 direction = Vector3.left;

void Update () {		
	if(transform.position.x <= xMin){
		direction = Vector3.right;
	} else if(transform.position.x >= xMax){
		direction = Vector3.left;
	}
	transform.position += direction * speed * Time.deltaTime;
}

Privacy & Terms