플레이어(Player)를 이동시켜 콜라이더와 부딪혔을 때, 원형체력바(Circle HealthBar)가 점점 감소하는 방법을 알아봅니다. UI의 Image 옵션인 Image Type(Filled), Fill Method(Radial 360), Fill Origin(Top), Fill Amount등을 이용하여 원형체력바를 만들어 봅니다. 유니티 2D게임 개발(Unity & C#)
#원형체력바만들기구현 #CircleHealthBar #RadialHP
[CircleGauge.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CircleGauge : MonoBehaviour
{
Image healthBar;
float maxHealth = 100f;
public static float health;
// Start is called before the first frame update
void Start()
{
healthBar = GetComponent<Image>();
health = maxHealth;
}
// Update is called once per frame
void Update()
{
healthBar.fillAmount = health / maxHealth;
}
}
[MoveMouse.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveMouse : MonoBehaviour
{
float moveX;
[SerializeField] [Range(100f, 800f)] float speed = 600f;
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
moveX = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
rb.velocity = new Vector2(moveX, rb.velocity.y);
}
}
[Cookie.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cookie : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
CircleGauge.health -= 10f;
}
}
댓글