【发布时间】:2020-08-26 07:21:39
【问题描述】:
我有一个简单的问题:如何避免翻转附在主角身上的血条?我不知道该怎么做。项目可以在这里下载:https://mega.nz/file/zUY0HYbL#ahwM_uGlp7-5iMLFjR1uaj6hgeVpjyhB3SCLe9xAt88
抱歉,我无法在此处附上。
【问题讨论】:
-
答案已准备就绪
我有一个简单的问题:如何避免翻转附在主角身上的血条?我不知道该怎么做。项目可以在这里下载:https://mega.nz/file/zUY0HYbL#ahwM_uGlp7-5iMLFjR1uaj6hgeVpjyhB3SCLe9xAt88
抱歉,我无法在此处附上。
【问题讨论】:
只要你有一个单一的健康栏值,比如一个决定健康栏大小的浮点数,你可以简单地在void Update()函数中添加这一行,它会修复健康栏翻转:
health = Mathf.Clamp(health, 0, maxHealth);
其中 health 是决定生命条长度和生命条比例的值。 (对不起,如果我只是不知道你在使用什么变量,你应该在我下次推荐时包含一段代码)并且 maxHealth 是最大玩家健康或健康条的最大比例。
[编辑] 把这行代码放到你的 Update 函数中。
currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
简单地说,它把 currentHealth 的值固定在 0 和 maxHealth 之间 所以它不能高于 maxHealth 并低于零。
祝你的项目好运。 :D
【讨论】:
你来了!
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);
}
}
【讨论】: