Laser Defender enemy is going to left side and not coming back

i m currently having trouble with lecture 108 in the lecture the enemy is bouncing
however when i try to do the same they are vibrating at the same place then i watched another lecture and it solved my problem but after writing the code the enemy is only moving to left side and not coming back here is a screenshot for that problem -


and here is my code for enemyspawner script -

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour {
	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.ViewportToScreenPoint(new Vector3(0, 0, distanceToCamera));
		Vector3 rightEdge = Camera.main.ViewportToScreenPoint(new Vector3(1, 0, distanceToCamera));
		xmax = rightEdge.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;
		}
		else {
			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  ) {
			movingRight = true;
		} else if (rightEdgeOfFormation > xmax) {
			movingRight = false;
		}
	}
}

please help me

Privacy & Terms