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