[Solved] I kill myself with the my own missiles

Hi everyone!

Just been wondering how to cope with this and not %100 of the solution.

Description:

At 14:00 of the video we are asked to type Debug.log to find out whether the enemy projectiles hit us. I’ve just found that there’s no response in console unless I press Spacebar three times and hence kill myself. Then, after watching the video again and thoroughly checked the problem, I noticed that the code for some reason makes my own projectiles pass through the console instead of the enemy’s.

Expected behaviour:

Enemies can actually kill me if hit by their projectiles.

Things I’ve tried:

To watch the video again in order to doublecheck procedures and to rename variables so PlayerLaser is different to Enemy’s Projectile.

Possible solution, don’t know how to proceed though:

I guess that I should find a way to distinguish in the code my laser and the Enemy’s missiles without creating a conflict.

Any help is most appreciated!

Player controller code:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
public GameObject laser;
public float speed = 15.0f;
public float padding = 0.5f;
public float projectileSpeed;
public float firingRate;
public float health = 250f;

float xmin;
float xmax; 

// Use this for initialization
void Start () {
	float distance = transform.position.z - Camera.main.transform.position.z; 
	Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance)); 
	Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1,0,distance));
	xmin = leftmost.x + padding;
	xmax = rightmost.x - padding;
	  
}
void Fire (){
	GameObject beam = Instantiate (laser, transform.position, Quaternion.identity) as GameObject; 
	beam.rigidbody2D.velocity = new Vector3 (0,projectileSpeed,0);
}
// Update is called once per frame
void Update () {
	if (Input.GetKeyDown(KeyCode.Space)) {
		InvokeRepeating("Fire",0.00001F, firingRate);  
	}
	
	if (Input.GetKeyUp(KeyCode.Space)) {
		CancelInvoke("Fire");
	}
	
	if (Input.GetKey(KeyCode.LeftArrow)) {
		//transform.position += new Vector3 (-speed * Time.deltaTime, 0f, 0f);
		transform.position += Vector3.left * speed * Time.deltaTime; 
	}
	else if (Input.GetKey(KeyCode.RightArrow)) {
		//transform.position += new Vector3 (speed * Time.deltaTime, 0f, 0f);
		transform.position += Vector3.right * speed * Time.deltaTime;
	} 
	
	// restrict player to the gamespace
	float newX = Mathf.Clamp (transform.position.x, xmin, xmax); 
	transform.position = new Vector3 (newX,transform.position.y, transform.position.z);
		
	}
	
void OnTriggerEnter2D(Collider2D collider){
	Projectile missile = collider.gameObject.GetComponent<Projectile>();
	if(missile){
		Debug.Log ("Player collided with a missile");
		health -= missile.GetDamage();
		missile.Hit ();
		if (health <= 0){
			Destroy (gameObject);
		}
	}  
}

}

EnemyBehaviour code:

using UnityEngine;
using System.Collections;

public class EnemyBehaviour : MonoBehaviour {

public float health = 150f; 
public GameObject projectile; 
public float projectileSpeed = 5.0f;

void Update () {
	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<Projectile>();
	if(missile){
		health -= missile.GetDamage();
		missile.Hit ();
		if (health <= 0){
			Destroy (gameObject);
		}
	}  
}

}

Cheers!

Juan

1 Like

the code for some reason makes my own projectiles pass through the console instead of the enemy’s.

hmmmm, it’s been years since I’ve touched the laser defender game. Just trying to guess from what you’ve said:

  1. So, Player projectiles get detected and log to console?
  2. But the enemy projectiles don’t show up on the console when collided?

My guess might be then that, the enemy projectiles, do they have that Projectile component? Or do the enemy projectiles have a collider at all to be detected by the player?

I guess that I should find a way to distinguish in the code my laser and the Enemy’s missiles without creating a conflict.

Yup, you’re on the right track regarding this :wink:

1 Like

pokedev8

You’re right mate! Someone told me the same thing and just corrected it. Now enemies can kill me and hits pass through the console.

Thanks for confirming my guess regarding the missile distintion thing, I owe you one!

Take care!

Have a lovely day!

1 Like

​Solution for killing myself was much easier. For those interested check last part of video 113. You simply add a new vector 3 to put the laser out of ship so laser don’t touch the spaceship collider.

1 Like