[SOLVED] The Enemies keep going right

Hi everyone! I am from Argentina so excuse my “potato English”

I am new, and I am stuck in this lesson of Laser Defender, because my enemies keep moving to the right
I read that some people have the same issue but they fixed it right.
I am not so lucky :frowning: I tried! everything and nothing happens.

Here is my “EnemySpawner” script. It’s similar to the one that is in the video.

public GameObject enemyPrefab;
public float width = 10f;
public float height = 5f;
public float speed = 5f;

private bool movingRight = true;
private float xmax;
private float xmin; 

// Use this for initialization
void Start () {
    float distanceToCamera = transform.position.z - Camera.main.transform.position.z;
    Vector3 leftBoundary = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distanceToCamera));
    Vector3 rightBoundary = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distanceToCamera));
    xmax = rightBoundary.x;
    xmin = leftBoundary.x; 
    foreach (Transform child in transform)
        GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
        enemy.transform.parent = child;
public void OnDrawGizmos()
    Gizmos.DrawWireCube(transform.position, new Vector3 (width, height));
// Update is called once per frame
void Update () {
	if (movingRight)
        transform.position += Vector3.right * speed * Time.deltaTime;
        transform.position += Vector3.left *-speed * Time.deltaTime;
    float rightEdgeOfFormation = transform.position.x + (0.5f * width);
    float leftEdgeOfFormation = transform.position.x - (0.5f * width);
    if (leftEdgeOfFormation < xmin || rightEdgeOfFormation > xmax)


I change the layout of the camera.

Well, that’s all.
Thank you everyone!

Your problem is that you have a double negative when you are aiming to move left as you are multiplying Vector3.left by -speed. Removing the negative should solve the issue.

Oh! That’s right. I don’t know why I left that minus there…

Thanks a lot!

Privacy & Terms