this is the code im using on the enemy spawner…as far as i can see my code is exactly like the course but it just makes my ships go back and forth so quickly they just kind of vibrate…when i turned the speed down i could see them move very briefly side to side…
any ideas…??
using UnityEngine;
using System.Collections;
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 leftEdge = Camera.main.ViewportToWorldPoint(new Vector3(0,0, distanceToCamera));
Vector3 rightEdge = Camera.main.ViewportToWorldPoint(new Vector3(1,0, distanceToCamera));
xmax = rightEdge.x;
xmin = leftEdge.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 || rightEdgeOfFormation >xmax);{
movingRight = !movingRight;
}
}
}