我有一个简单的问题:如何避免将附在主角上的保健栏翻转?我不知道该怎么做。您可以在此处下载的项目:https://mega.nz/file/zUY0HYbL#ahwM_uGlp7-5iMLFjR1uaj6hgeVpjyhB3SCLe9xAt88
抱歉,我无法在此处附加它。
只要您对运行状况栏有一个单一值,例如确定运行状况栏比例的浮点数,您只需在void Update()
函数中添加此行,即可固定运行状况栏,使其不会翻转:
health = Mathf.Clamp(health, 0, maxHealth);
Where health是确定健康条长度和健康条比例的值。 (对不起,如果让我感到困惑,我只是不知道您使用的是什么变量,下次我建议您应该包含一段代码),其中maxHealth是最大玩家健康状况或healthbar的最大比例。
你在这里!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public Healthbar healthBar;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
TakeDamage(20);
}
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
}