using UnityEngine;
using System.Collections;
public class PlayerHealth01 : MonoBehaviour
{
public float health = 100f; // The player's health.
public float repeatDamagePeriod = 2f; // How frequently the player can be damaged.
public float damageAmount = 10f; // The amount of damage to take when enemies touch the player
private float lastHitTime; // The time at which the player was last hit.
private PlayerControl playerControl; // Reference to the PlayerControl script.
void Awake ()
{
// Setting up references.
playerControl = GetComponent<PlayerControl>();
}
void OnCollisionEnter2D (Collision2D col)
{
// If the colliding gameobject is an Enemy...
if(col.gameObject.tag == "Enemy")
{
// ... and if the time exceeds the time of the last hit plus the time between hits...
if (Time.time > lastHitTime + repeatDamagePeriod)
{
// ... and if the player still has health...
if(health > 0f)
{
// ... take damage and reset the lastHitTime.
print (" Hero Damaged");
//TakeDamage(col.transform);
lastHitTime = Time.time;
}
}
}
}
}
현재 시간(Time.time)이 과거시간(lastHitTime) + 설정시간(repeatDamagePeriod) 보다 크면 "Hero Damaged"를 출력
repeatDamagePeriod가 2로 설정되어 있으면, 한번 적캐릭터와 부딪힌 후 2초 후에 다시 부딪히고 있다면 에너지를 감소시킴
using UnityEngine;
using System.Collections;
public class PlayerHealth02 : MonoBehaviour
{
public float health = 100f; // The player's health.
public float repeatDamagePeriod = 2f; // How frequently the player can be damaged.
public float hurtForce = 10f; // The force with which the player is pushed when hurt.
public float damageAmount = 10f; // The amount of damage to take when enemies touch the player
private SpriteRenderer healthBar; // Reference to the sprite renderer of the health bar.
private float lastHitTime; // The time at which the player was last hit.
private Vector3 healthScale; // The local scale of the health bar initially (with full health).
private PlayerControl playerControl; // Reference to the PlayerControl script.
void Awake ()
{
// Setting up references.
playerControl = GetComponent<PlayerControl>();
healthBar = GameObject.Find("HealthBar").GetComponent<SpriteRenderer>();
// Getting the intial scale of the healthbar (whilst the player has full health).
healthScale = healthBar.transform.localScale;
}
void OnCollisionEnter2D (Collision2D col)
{
// If the colliding gameobject is an Enemy...
if(col.gameObject.tag == "Enemy")
{
// ... and if the time exceeds the time of the last hit plus the time between hits...
if (Time.time > lastHitTime + repeatDamagePeriod)
{
// ... and if the player still has health...
if(health > 0f)
{
// ... take damage and reset the lastHitTime.
//print (" Hero Damaged");
TakeDamage(col.transform);
lastHitTime = Time.time;
}
}
}
}
void TakeDamage (Transform enemy)
{
// Make sure the player can't jump.
playerControl.jump = false;
// Create a vector that's from the enemy to the player with an upwards boost.
Vector3 hurtVector = transform.position - enemy.position + Vector3.up * 5f;
// Add a force to the player in the direction of the vector and multiply by the hurtForce.
rigidbody2D.AddForce(hurtVector * hurtForce);
// Reduce the player's health by 10.
health -= damageAmount;
// Update what the health bar looks like.
UpdateHealthBar();
}
public void UpdateHealthBar ()
{
// Set the health bar's colour to proportion of the way between green and red based on the player's health.
healthBar.material.color = Color.Lerp(Color.green, Color.red, 1 - health * 0.01f);
// Set the scale of the health bar to be proportional to the player's health.
healthBar.transform.localScale = new Vector3(healthScale.x * health * 0.01f, 1, 1);
}
}
rigidbody2D.AddForce()함수를 이용해서 적캐릭터와 부딪히면 위쪽으로 튕기도록 하고 있음
localScale을 이용해서 Health바의 크기를 조절해서 표현하고 있음
health 값이 감소함에 따라 material.color를 연두색에서 빨간색으로 변하도록 하고 있음
Color.Lerp() : 보간 값을 찾는 함수/ 여기서는 그린과 레드상의 한값을 찾게됨
using UnityEngine;
using System.Collections;
public class PlayerHealth03 : MonoBehaviour
{
public float health = 100f; // The player's health.
public float repeatDamagePeriod = 2f; // How frequently the player can be damaged.
public AudioClip[] ouchClips; // Array of clips to play when the player is damaged.
public float hurtForce = 10f; // The force with which the player is pushed when hurt.
public float damageAmount = 10f; // The amount of damage to take when enemies touch the player
private SpriteRenderer healthBar; // Reference to the sprite renderer of the health bar.
private float lastHitTime; // The time at which the player was last hit.
private Vector3 healthScale; // The local scale of the health bar initially (with full health).
private PlayerControl playerControl; // Reference to the PlayerControl script.
void Awake ()
{
// Setting up references.
playerControl = GetComponent<PlayerControl>();
healthBar = GameObject.Find("HealthBar").GetComponent<SpriteRenderer>();
// Getting the intial scale of the healthbar (whilst the player has full health).
healthScale = healthBar.transform.localScale;
}
void OnCollisionEnter2D (Collision2D col)
{
// If the colliding gameobject is an Enemy...
if(col.gameObject.tag == "Enemy")
{
// ... and if the time exceeds the time of the last hit plus the time between hits...
if (Time.time > lastHitTime + repeatDamagePeriod)
{
// ... and if the player still has health...
if(health > 0f)
{
// ... take damage and reset the lastHitTime.
//print (" Hero Damaged");
TakeDamage(col.transform);
lastHitTime = Time.time;
}
}
}
}
void TakeDamage (Transform enemy)
{
// Make sure the player can't jump.
playerControl.jump = false;
// Create a vector that's from the enemy to the player with an upwards boost.
Vector3 hurtVector = transform.position - enemy.position + Vector3.up * 5f;
// Add a force to the player in the direction of the vector and multiply by the hurtForce.
rigidbody2D.AddForce(hurtVector * hurtForce);
// Reduce the player's health by 10.
health -= damageAmount;
// Update what the health bar looks like.
UpdateHealthBar();
// Play a random clip of the player getting hurt.
int i = Random.Range (0, ouchClips.Length);
AudioSource.PlayClipAtPoint(ouchClips[i], transform.position);
}
public void UpdateHealthBar ()
{
// Set the health bar's colour to proportion of the way between green and red based on the player's health.
healthBar.material.color = Color.Lerp(Color.green, Color.red, 1 - health * 0.01f);
// Set the scale of the health bar to be proportional to the player's health.
healthBar.transform.localScale = new Vector3(healthScale.x * health * 0.01f, 1, 1);
}
}
using UnityEngine;
using System.Collections;
public class PlayerHealth04 : MonoBehaviour
{
public float health = 100f; // The player's health.
public float repeatDamagePeriod = 2f; // How frequently the player can be damaged.
public AudioClip[] ouchClips; // Array of clips to play when the player is damaged.
public float hurtForce = 10f; // The force with which the player is pushed when hurt.
public float damageAmount = 10f; // The amount of damage to take when enemies touch the player
private SpriteRenderer healthBar; // Reference to the sprite renderer of the health bar.
private float lastHitTime; // The time at which the player was last hit.
private Vector3 healthScale; // The local scale of the health bar initially (with full health).
private PlayerControl playerControl; // Reference to the PlayerControl script.
private Animator anim; // Reference to the Animator on the player
void Awake ()
{
// Setting up references.
playerControl = GetComponent<PlayerControl>();
healthBar = GameObject.Find("HealthBar").GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
// Getting the intial scale of the healthbar (whilst the player has full health).
healthScale = healthBar.transform.localScale;
}
void OnCollisionEnter2D (Collision2D col)
{
// If the colliding gameobject is an Enemy...
if(col.gameObject.tag == "Enemy")
{
// ... and if the time exceeds the time of the last hit plus the time between hits...
if (Time.time > lastHitTime + repeatDamagePeriod)
{
// ... and if the player still has health...
if(health > 0f)
{
// ... take damage and reset the lastHitTime.
//print (" Hero Damaged");
TakeDamage(col.transform);
lastHitTime = Time.time;
}
}
// If the player doesn't have health, do some stuff, let him fall into the river to reload the level.
else
{
// Find all of the colliders on the gameobject and set them all to be triggers.
Collider2D[] cols = GetComponents<Collider2D>();
foreach(Collider2D c in cols)
{
c.isTrigger = true;
}
// Move all sprite parts of the player to the front
SpriteRenderer[] spr = GetComponentsInChildren<SpriteRenderer>();
foreach(SpriteRenderer s in spr)
{
s.sortingLayerName = "UI";
}
// ... disable user Player Control script
GetComponent<PlayerControl>().enabled = false;
// ... disable the Gun script to stop a dead guy shooting a nonexistant bazooka
GetComponentInChildren<Gun>().enabled = false;
// ... Trigger the 'Die' animation state
anim.SetTrigger("Die");
}
}
}
void TakeDamage (Transform enemy)
{
// Make sure the player can't jump.
playerControl.jump = false;
// Create a vector that's from the enemy to the player with an upwards boost.
Vector3 hurtVector = transform.position - enemy.position + Vector3.up * 5f;
// Add a force to the player in the direction of the vector and multiply by the hurtForce.
rigidbody2D.AddForce(hurtVector * hurtForce);
// Reduce the player's health by 10.
health -= damageAmount;
// Update what the health bar looks like.
UpdateHealthBar();
// Play a random clip of the player getting hurt.
int i = Random.Range (0, ouchClips.Length);
AudioSource.PlayClipAtPoint(ouchClips[i], transform.position);
}
public void UpdateHealthBar ()
{
// Set the health bar's colour to proportion of the way between green and red based on the player's health.
healthBar.material.color = Color.Lerp(Color.green, Color.red, 1 - health * 0.01f);
// Set the scale of the health bar to be proportional to the player's health.
healthBar.transform.localScale = new Vector3(healthScale.x * health * 0.01f, 1, 1);
}
}
죽을 때 처리 Hero 처리
모든 Collider의 Trigger를 True로 세팅해서 충돌 체크를 하지 않게 만듬
Renderer를 맨 앞쪽 Layer로 설정
playerControl.cs 및 Gun.cs를 비활성화함
using UnityEngine;
using System.Collections;
public class Remover01 : MonoBehaviour
{
public GameObject splash;
void OnTriggerEnter2D (Collider2D col)
{
// ... instantiate the splash where the player falls in.
Instantiate (splash, col.transform.position, transform.rotation);
// ... destroy the player.
Destroy (col.gameObject);
// ... reload the level.
}
}
using UnityEngine;
using System.Collections;
public class Remover02 : MonoBehaviour {
public GameObject splash;
void OnTriggerEnter2D(Collider2D col)
{
// If the player hits the trigger...
if(col.gameObject.tag == "Player")
{
// ... instantiate the splash where the player falls in.
Instantiate(splash, col.transform.position, transform.rotation);
// ... destroy the player.
Destroy (col.gameObject);
// ... reload the level.
StartCoroutine("ReloadGame");
}
else
{
// ... instantiate the splash where the enemy falls in.
Instantiate(splash, col.transform.position, transform.rotation);
// Destroy the enemy.
Destroy (col.gameObject);
}
}
IEnumerator ReloadGame()
{
// ... pause briefly
yield return new WaitForSeconds(2);
// ... and then reload the level.
Application.LoadLevel(Application.loadedLevel);
}
}
using UnityEngine;
using System.Collections;
public class HealthPickup01 : MonoBehaviour {
private Animator anim; // Reference to the animator component.
private bool landed; // Whether or not the crate has landed.
void Awake ()
{
// Setting up the references.
anim = transform.root.GetComponent<Animator>();
}
void OnTriggerEnter2D (Collider2D other)
{
print ("Collision Test");
if(other.tag == "ground" && !landed)
{
// ... set the Land animator trigger parameter.
anim.SetTrigger("Land");
transform.parent = null;
gameObject.AddComponent<Rigidbody2D>();
landed = true;
}
}
}