Lasers Spawning on Top of enemy killing them instantly

As soon as I press the play button my enemies die instantaneously after I saw a laser on top of it. I checked my Z coordinates many times but I still could not figure out the problem. Here is my code:
public class EnemyBehavior : MonoBehaviour {
public GameObject projectile;
public float projectileSpeed = 10;
public float health = 150;

void Update (){
	Vector3 startPosition = transform.position + new Vector3(0, -1, 0);
	GameObject missle = Instantiate (projectile, transform.position, Quaternion.identity) as GameObject;
	missle.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projectileSpeed);
}

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

}

Are their no explanations? I cannot move forward until this is fixed.

I’m having this same problem, I think it might have something to do with the lasers spawning directly on the ships, but I can’t seem to get the offset to work either.

I had the same problem and found that if you essentially combine:
Vector3 startPosition = transform.position + new Vector(0, -1, 0);
and
GameObject missle = Instantiate (projectile, transform.position, Quaternion.identity);
you can use
GameObject missle = Instantiate (projectile, transform.position + new Vector(0, -1, 0) , Quaternion.identity);

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms