// Once the projectile collides with any object, only collides with ships void OnCollisionEnter2D(Collision2D collision) { GameObject collidedObject = collision.gameObject; //Debug.Log("Collided with " + collidedObject.name); //ignore any collisions with the owner ship just in case if (collidedObject.gameObject.Equals(ownerShip)) { return; } if (collidedObject.gameObject.tag == "ship") //check if the collided object is a ship { try { //Try getting the Player Ships' Script PlayerShipBehaviour collidedScript = collidedObject.GetComponent <PlayerShipBehaviour>(); collidedScript.health -= damage; //decrease player's health Destroy(gameObject); } catch (NullReferenceException) { //Get Ship's Script if player ship script doesn't exist ShipBehaviour collidedScript = collidedObject.GetComponent <ShipBehaviour>(); collidedScript.health -= damage; //decrease ships' health Destroy(gameObject); } } //Maybe useful in the future - different kind of weapon? - ignores further collisions with the collidedObject Physics2D.IgnoreCollision(GetComponent <Collider2D>(), collidedObject.GetComponent <Collider2D>(), true); }
//"new" overrides the superclass (ShipBehaviour) function new void Start() { projectileObject = Resources.Load <GameObject>("GameObjects/Projectile") as GameObject; //loads projectile object //creates key bindings //In future, can check for existing key bindings and load them //If key bind doesn't exist, load in these ones: keybinds.Add("Forward", KeyCode.W); keybinds.Add("Backward", KeyCode.S); keybinds.Add("Rotate Left", KeyCode.A); keybinds.Add("Rotate Right", KeyCode.D); keybinds.Add("Fire", KeyCode.Space); keybinds.Add("Map", KeyCode.M); //static attribute used to reference the players ship by using PlayerShipBehaviour.instance instance = this; }