PS. this game requires sound to be playable.
Pieces of my code used to make this
using UnityEngine;
using System.Collections;
public class BossMovement : MonoBehaviour {
public int aggresionState = 0; // 0= passive, 1= set up, 2= first attack phase, 3= second attack phase
public int health = 200; // boss health is set in the unity editor
public float cooldownShooting = 0; // time between shots
public float spawnTime;
//gameobjects
public GameObject player;
public GameObject Bullet, PrefabBullet;
public GameObject bossWall, bossWallPrefab;
public Rigidbody2D rb;
public AudioClip snd_gameover;
// Use this for initialization
void Start() {
player = GameObject.Find("Player");
rb = GetComponent();
// create a defensive wall, place it and deactivate it untill aggresionState ==3
bossWallPrefab = Instantiate(bossWall, transform.position + new Vector3(0,-1.5f), transform.rotation) as GameObject;
bossWallPrefab.transform.localScale= new Vector3(40f, 2f, 0f);
bossWallPrefab.gameObject.SetActive(false);
}
// Update is called once per frame
void Update() {
if (aggresionState == 0) {
// if player is within range of the boss set aggresionState to 1
if ((player.transform.position - transform.position).magnitude < 5) {
aggresionState = 1;
}
}
// dramatic boss walk untill he reaches position
if (aggresionState == 1) {
Walk();
}
if (aggresionState == 2) {
// a small internal clock that shoots a bullet every 0.5 seconds
cooldownShooting += Time.deltaTime;
if (cooldownShooting > 0.5) {
cooldownShooting -= 0.5f;
Shoot();
}
}
if (aggresionState == 3) {
// moves the boss back to top
Move3();
// activate the wall when the player approaches
Wall1();
// internal clock that shoots a modified bullet every second
cooldownShooting += Time.deltaTime;
if (cooldownShooting > 1) {
cooldownShooting -= 1f;
Shoot3();
}
}
if (aggresionState == 4) {
// let the boss accept his defeat
}
}
void Walk() { // set boss to his desired position
rb.velocity = new Vector2(0, -0.5f);
// if position is reached set aggresionState to 2
if (transform.position.magnitude < 2) {
aggresionState = 2;
rb.velocity = Vector2.zero;
}
}
void Move3() {
rb.velocity = new Vector2(0, 1f);
if (transform.position.magnitude > 5) {
rb.velocity = Vector2.zero;
}
}
// shoots a regular bullet
void Shoot() {
PrefabBullet = Instantiate(Bullet, transform.position, transform.rotation) as GameObject;
PrefabBullet.transform.localScale = new Vector3(2f, 2f, 1f);
Rigidbody2D bulletprefab = PrefabBullet.GetComponent();
bulletprefab.velocity = Vector3.Normalize(player.transform.position - transform.position) * 4f;
}
// shoots a modified bullet that is wider
void Shoot3() {
PrefabBullet = Instantiate(Bullet, transform.position, transform.rotation) as GameObject;
PrefabBullet.transform.localScale = new Vector3(16f, 2f, 1f);
Rigidbody2D bulletprefab = PrefabBullet.GetComponent();
bulletprefab.velocity = Vector3.Normalize(player.transform.position - transform.position) * 4f;
}
void Wall1() {
// if the player is too close to the boss -> activate the wall
if ((player.transform.position - transform.position).magnitude < 3 ) {
bossWallPrefab.gameObject.SetActive(true);
bossWallPrefab.transform.position = transform.position + new Vector3(0, -1.5f);
} else { // if player is not too close to the boss -> deactivate the wall
bossWallPrefab.gameObject.SetActive(false);
}
}
void OnCollisionEnter2D(Collision2D collision) {
// if the boss is in a vulnerable state and is hit by a bullet
if (aggresionState >= 2) {
if (collision.gameObject.tag == "PlayerBullet") {
Destroy(collision.gameObject);
health--;
}
}
if (health == 100) {
aggresionState = 3;
}
else if (health == 50) {
aggresionState = 4;
bossWallPrefab.gameObject.SetActive(false); // deactivate the wall since the boss doesn't do anything anymore
}
else if (health == 0) {
gameObject.SetActive(false); // "destroys" the boss
AudioSource.PlayClipAtPoint(snd_gameover, transform.position, 1f); // end the game
}
}
}
/*
This is a script that both moves the wall and rotates wall, without directly rotating the wall.
In the FixedUpdate the wall is being placed between the player and the greenEnemy at a fixed distance,
but since the greenEnemy and the wall can't be on the same place, Unity automaticaly pushes the wall so that the wall doesn't overlap the greenEnemy.
This way it looks like the wall is rotating around the greenEnemy.
*/
using UnityEngine;
using System.Collections;
public class GreenWallScript : MonoBehaviour {
public GameObject greenEnemy, player;
// Use this for initialization
void Start () {
player = GameObject.Find("Player");
}
// Update is called once per frame
void FixedUpdate () {
transform.position = (player.transform.position - greenEnemy.transform.position).normalized *0.8f+ greenEnemy.transform.position;
}
}
HIDE CONTENT GOES HERE