본문 바로가기
게임개발/게임플랫폼 응용 프로그래밍

좀비 서바이버 (3) 플레이어 체력 UI 만들기

by pudding81 2024. 3. 8.

 

 

슬라이더의 크기 - 캔버스 아래를 모두 선택한 후

앵커프리셋 >>  alt누르고 우측하단 stretch 클릭

 

 

using UnityEngine;
using UnityEngine.UI; // UI 관련 코드

// 플레이어 캐릭터의 생명체로서의 동작을 담당
public class PlayerHealth : LivingEntity {
    public Slider healthSlider; // 체력을 표시할 UI 슬라이더

    public AudioClip deathClip; // 사망 소리
    public AudioClip hitClip; // 피격 소리
    public AudioClip itemPickupClip; // 아이템 습득 소리

    private AudioSource playerAudioPlayer; // 플레이어 소리 재생기
    private Animator playerAnimator; // 플레이어의 애니메이터

    private PlayerMovement playerMovement; // 플레이어 움직임 컴포넌트
    private PlayerShooter playerShooter; // 플레이어 슈터 컴포넌트

    private void Awake() {
        // 사용할 컴포넌트를 가져오기
        playerAnimator = GetComponent<Animator>();
        playerMovement = GetComponent<PlayerMovement>();    
        playerAudioPlayer = GetComponent<AudioSource>();
        playerShooter = GetComponent<PlayerShooter>();


    }

    protected override void OnEnable() {
        // LivingEntity의 OnEnable() 실행 (상태 초기화)
        base.OnEnable();

        healthSlider.gameObject.SetActive(true);

        healthSlider.maxValue = startingHealth;
        healthSlider.value = health; //현재 체력값

        playerMovement.enabled = true;
        playerShooter.enabled = true;   


    }

    // 체력 회복
    public override void RestoreHealth(float newHealth) {
        // LivingEntity의 RestoreHealth() 실행 (체력 증가)
        base.RestoreHealth(newHealth);

        healthSlider.value = health;


    }

    // 데미지 처리
    public override void OnDamage(float damage, Vector3 hitPoint, Vector3 hitDirection) {
       
        if(!dead)
        {
            playerAudioPlayer.PlayOneShot(hitClip);
        }
  
        // LivingEntity의 OnDamage() 실행(데미지 적용)
        base.OnDamage(damage, hitPoint, hitDirection);
        healthSlider.value = health;


    }

    // 사망 처리
    public override void Die() {
        // LivingEntity의 Die() 실행(사망 적용)
        base.Die();

        healthSlider.gameObject.SetActive(false);

        playerAudioPlayer.PlayOneShot(deathClip);
        playerAnimator.SetTrigger("Die");

        playerMovement.enabled = false;
        playerShooter.enabled = false;



    }

    private void OnTriggerEnter(Collider other) {

        if (!dead)
        {
            IItem item = other.GetComponent<IItem>();

            if (item != null)
            {
                item.Use(gameObject);
                playerAudioPlayer.PlayOneShot(itemPickupClip);
            }
        }
        
        
        // 아이템과 충돌한 경우 해당 아이템을 사용하는 처리
    }
}

'게임개발 > 게임플랫폼 응용 프로그래밍' 카테고리의 다른 글

스프라이트 이미지 선명하게 바꾸기  (0) 2024.03.08
좀비 서바이버 (4) 좀비 AI  (0) 2024.03.08
collider.ColsetPoint  (0) 2024.03.08
OverlapSphere  (0) 2024.03.08
Navigation AI 이동하기  (0) 2024.03.08