因此,在某些情况下,我制作了一个运行状况栏,该栏在52秒(等待52秒,然后从0运行状况变为100运行状况)之后加载,现在我也创建了检查点。两者都可以单独正常工作,但是当我死了时,运行状况栏动画会重置,我必须等待52秒才能看到运行状况栏。当您死亡并在检查点重新生成时,我尝试禁用了动画制作器,但由于它被我的健康条脚本覆盖,因此无法正常工作。这是我的healthbar脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Healthbar : MonoBehaviour
{
[SerializeField] private Slider slider;
[SerializeField] private Animator anim;
private float Waittime = 52f;
public GameObject player;
public void SetMaxHealth(int health)
{
slider.maxValue = health;
slider.value = health;
}
public void SetHealth(int health)
{
slider.value = health;
}
private void Update()
{
if (Waittime <= 0)
{
anim.enabled = false;
}
else
{
Waittime -= Time.deltaTime;
anim.enabled = true;
}
}
}
这是我尝试在播放器从检查点重生后禁用动画器的功能(可能会有帮助:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class healthafter : MonoBehaviour
{
// Start is called before the first frame update
private GameMaster GM;
public Animator anim;
void Start()
{
GM = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
transform.position = GM.lastCheckpointPos;
anim.enabled = false;
}
}
播放器从检查点重生后,如何禁用动画?
此代码可以改进...但是在您当前的情况下,您也必须重置waitTime变量。因此,heathafter脚本中的启动函数应如下所示。
public HealthBar healthbar;
void Start()
{
GM = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
transform.position = GM.lastCheckpointPos;
anim.enabled = false;
healthbar.ResetWaitTime(); // this will call ResetWaitTime method from your healthbar script
}
将此添加到您的HealthBar脚本中
public void ResetWaitTime(){
Waittime =52;
}