PART 2 STARTS HERE.
CODING THE ENEMY
I was given the task to also code the enemy's behaviour: Death, Patrol/Movement , Damage to Character, Health.
The first code presented is for its movement. Basically it gives the enemy the ability to walk around the platform and "react" to an edge when it reaches it. When it reaches the edge, it will turn back and continue on its set path and repeat the process. The same goes for the wall of a platform. It "senses" the collider of the wall, and when the enemy reaches it, it makes it turn turn around the same way.
using UnityEngine;
using System.Collections;
public class EnemyPatrol : MonoBehaviour {
public float moveSpeed;
public bool moveRight;
public Transform wallCheck;
public float wallCheckRadius;
public LayerMask whatisWall;
private bool hitWall;
private bool notatEdge;
public Transform edgeCheck;
void Update ()
{
hitWall = Physics2D.OverlapCircle (wallCheck.position, wallCheckRadius, whatisWall);
notatEdge = Physics2D.OverlapCircle (edgeCheck.position, wallCheckRadius, whatisWall);
if (hitWall || !notatEdge)
moveRight = !moveRight;
if (moveRight) {
transform.localScale = new Vector3(-0.1395238f,0.1395238f,0.1395238f);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D> ().velocity.y);
}
else
{
transform.localScale = new Vector3(0.1395238f,0.1395238f,0.1395238f);
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed,GetComponent<Rigidbody2D>().velocity.y);
}
}
______________________________
The second code is giving the enemy a "health bar". It gives the enemy a specific amount of health and if the player jumps on it, it is destroyed and it plays a destruction effect on it. The code makes the slime give points to the player when destroyed. Also it allows it damage the player if it is hit as if it was a poisonous slime.
using UnityEngine;
using System.Collections;
public class EnemyHealth_Manager : MonoBehaviour
{
public int enemyHealth;
public GameObject deathEffect;
public int pointsOnDeath;
void Update ()
{
if (enemyHealth <= 0)
{
Instantiate(deathEffect, transform.position, transform.rotation);
Score_Manager.AddPoints(pointsOnDeath);
Destroy(gameObject);
}
}
public void giveDamage(int damagetoGive)
{
enemyHealth -= damagetoGive;
}
}
______________________________
The code shown below, allows the player to re-spawn with full health when they are killed by a slime. The Level Manager allows the player to re-spawn.
using UnityEngine;
using System.Collections;
public class Kill_Player : MonoBehaviour {
public Level_Manager levelManager;
// Use this for initialization
void Start ()
{
levelManager = FindObjectOfType<Level_Manager> ();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Player")
{
levelManager.RespawnPlayer ();
}
}
}
______________________________
The fourth code determines how much damage the slime will do to the player if the player does not successfully hit the slime and knocking it away from while doing so.
using UnityEngine;
using System.Collections;
public class Hurt_Player_OnHit : MonoBehaviour {
public int damageTogive;
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Player")
{
Health_Manager.HurtPlayer(damageTogive);
var player = other.GetComponent<PlayerController>();
player.knockbackCount = player.knockbackLenght;
if(other.transform.position.x < transform.position.x)
player.knockfromRight = true;
else
player.knockfromRight = false;
}
}
}
______________________________
The last code was initially used as a Health Manager System for the slime before knowing how to properly add a code for it. After some tutorial I found online, I managed to get that behavior working with the previous code mentioned. It serves the exact same purpose as the other code; how much damage it will receive from the player, only this one is not as great but still functional.
using UnityEngine;
using System.Collections;
public class HurtEnemyOnHit : MonoBehaviour {
int damage = 5;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
GetComponent<EnemyHealth_Manager> ().giveDamage (damage);
}
}
}
END OF PART 2