Health - > 체력
repeatDamagePeriod -> 데미지를 받고나서 다시한번 받을때 까지 간격
ouch clips -> 아픈 사운드 종류별로;
Hurt Force -> 플레이어와 적 충돌시 발생되는 힘
Damage Amount -> 깍이는 체력양
healthBar -> 체력 바는 피봇 위치를 왼쪽으로 해준다.
healthScale -> 이값을 이용하여 체력 게이지의 크기를 줄여줌
OnCollisionEnter2D -> 캐릭터와 충돌 발생시
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.
TakeDamage(col.transform);
lastHitTime = Time.time;
}
}
}
Enemy 태그와 충돌시
그리고 repeatDamagePeriod 간격이 지날때만 실행 -> 인스펙터 창에서 조절 가능
체력이 0보다 클때는 TakeDamage 함수 실행
// 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");
}
체력이 0보다 작을때는(죽을때) 모든 물체의 충돌을 무시하기 위해서 모든 충돌체를 isTrigger 해주고
모든 스프라이트 렌더러의 레이어 값을 UI 로 설정해줌->최상위 오브젝트로(맨위에 보이게)
그리고 PlayerControl 스크립트를 꺼주어 캐릭터가 못움직이게 한다
Gun 스크립트를 꺼주어서 총을 발사를 못하게 한다.
마지막으로 죽는 애니메이션을 실행시킨다.
플레이어가 충돌시 실행되는 함수
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);
}
충돌발생시 점프를 못하게하고
적과 부딪힌 방향으로 캐릭터를 튕기게 해준다. -> rigidbody2D.AddForce(hurtVector * hurtForce);
그리고 체력을 깍고 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);
}
체력이 깍일수록 점점 빨간색으로 변함.
health 값으로 체력바의 길이 조절
체력바 에 붙는 스크립트
주인공위에 항상 따라다님.
ui_healthDisplay
'잡다한것들전부 > 공식예제' 카테고리의 다른 글
| 물체 파괴 함수 -> Destroyer 스크립트 (0) | 2014.02.25 |
|---|---|
| killTrigger -> 모든 적과 주인공 충돌(강) - Removver 스크립트 (0) | 2014.02.25 |
| 플레이어의 체력 - PlayerHealth (0) | 2014.02.25 |
| 플레이어 무기 발사 - gun 스크립트 (0) | 2014.02.25 |
| 주인공을 따라다니는 카메라 (0) | 2014.02.25 |
| 플레이어 컨트롤 스크립트 (0) | 2014.02.25 |


