A couple of issues after adding starfield for Laser Defender

hi
I had a couple of issues after adding the starfield, I think it is just a coincidence but thought stating that should be relevant.
One issue is : enemy spawner only spawns 3 out of 5.
2nd issue is: while in play mode enemy ships intermittently “shake” or look like they “vibrate”


using UnityEngine;
using System.Collections;

public class EnemySpawner : MonoBehaviour {

public GameObject enemyPrefab;
public float width = 10f;
public float height = 10f;
public float speed = 5f;
public float spawnDelay = 0.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 leftBoundry =  Camera.main.ViewportToWorldPoint(new Vector3(0,0, distanceToCamera));
	Vector3 rightBoundry =  Camera.main.ViewportToWorldPoint(new Vector3(1,0, distanceToCamera));
	xmax = rightBoundry.x;
	xmin = leftBoundry.x;
	SpawnUntilFull(); 

}
void SpawnEnemies() {
	foreach( Transform child in transform) {
		GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
		enemy.transform.parent = child;       			
	}
	
}

void SpawnUntilFull(){
     Transform freePosition = NextFreePosition();
     if(freePosition){
	      GameObject enemy = Instantiate(enemyPrefab, freePosition.position, Quaternion.identity) as GameObject;
	      enemy.transform.parent = NextFreePosition();
     }
	if (NextFreePosition()){	
	Invoke ("SpawnUntilFull", spawnDelay);	
	}

}

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;	
	}
	if(AllMembersDead()){

	Debug.Log ("Empty Formation");
		SpawnUntilFull();
	}

}
Transform NextFreePosition(){
foreach(Transform childPositionGameObject in transform){
if (childPositionGameObject.childCount == 0){
return childPositionGameObject;
}
}
return null;
}

bool AllMembersDead(){
  
 foreach(Transform childPositionGameObject in transform){
		if	(childPositionGameObject.childCount > 0){
		    return false;
		}
    }
    return true;
    }
       

       
}


using UnityEngine;
using System.Collections;

public class EnemyBehavior : MonoBehaviour {

public GameObject projectile;
public float projectileSpeed = 10;
public float health = 150;
public float shotsPerSeconds = 0.5f;

void Update(){
    float probability = Time.deltaTime * shotsPerSeconds;
    if(Random.value < probability){
	 Fire ();
    }
    

}
void Fire()	{
	Vector3 startPosition = transform.position + new Vector3(0, -1, 0);
   GameObject missile = Instantiate(projectile, startPosition, Quaternion.identity) as GameObject;
   missile.rigidbody2D.velocity = new Vector2(0, -projectileSpeed);}

void OnTriggerEnter2D(Collider2D collider){
Projectile missile = collider.gameObject.GetComponent();
if (missile){
health -= missile.GetDamage();
missile.Hit();
if (health <= 0){
Destroy (gameObject);
}

    } 

}
}


hi
so far no response, but I will add that I tried deleting and re installing Enemy spawner to no avail. the game would play with one enemy then after being shot a few times and re appearing then 2 would show and then 3. I have somehow managed to get 5 to appear and here is a screenshot. the actual enemy ships are NOT at the placement. IMG_1991

I think the first thing we need to do is clean up a bit of your code to eliminate the possibility of any weird behavior occurring as a result.

  1. In your EnemySpawner code, the Vector3 inside the OnDrawGizmos() method doesn’t specify the Z value.

public void OnDrawGizmos() {

Gizmos.DrawWireCube(transform.position, new Vector3(width, height));
}

If you don’t want it to change, put in a zero after height so it looks like this:

Gizmos.DrawWireCube(transform.position, new Vector3(width, height, 0));

  1. In your EnemySpawner code inside the Update() method, you aren’t clamping the sides of the game space to prevent the Enemy formation or the Player from going outside of it.

float rightEdgeOfFormation = transform.position.x + (0.5fwidth);
float leftEdgeOfFormation = transform.position.x - (0.5f
width);

Create a float that uses Mathf.clamp to clamp the left and right viewport values so the EnemyFormation cannot pass outside the gamespace walls.

float xnew = Mathf.Clamp(transform.position.x, xmin, xmax);

Then, using this xnew value, finalize the calculations of the right and left walls of the Enemy formation and move the Enemy formation appropriately.

transform.position = new Vector3(xnew, transform.position.y, transform.position.z);

  1. In the EnemySpawner code in the Update() method, you aren’t calculating when the Enemy formation makes contact with either edge using less than or equal to / greater than or equal to, as you are just using less than / greater than.

if (leftEdgeOfFormation < xmin){
movingRight = true;
}else if (rightEdgeOfFormation >xmax){
movingRight = false;
}

Try this instead:

if (transform.position.x <= xmin) {
movingRight = true;
} else if (transform.position.x >= xmax) {
movingRight = false;
}

  1. In your EnemySpawner code in the SpawnUntilFull() method, change the way the NextFreePosition() method invokes “SpawnUntilFull”.

void SpawnUntilFull(){
Transform freePosition = NextFreePosition();
if(freePosition){
GameObject enemy = Instantiate(enemyPrefab, freePosition.position, Quaternion.identity) as GameObject;
enemy.transform.parent = NextFreePosition();
}
if (NextFreePosition()){
Invoke (“SpawnUntilFull”, spawnDelay);
}

Try this instead:

public void SpawnUntilFull() {
Transform freePosition = NextFreePosition();
if (freePosition) {
GameObject enemy = Instantiate(enemyPrefab, freePosition.position, Quaternion.identity) as GameObject;
enemy.transform.parent = freePosition;
Invoke(“SpawnUntilFull”, spawnDelay);
}
}

Can you make these changes for me and let me know if this fixes your issues?

Privacy & Terms