Unity Slider填充在开始时不断消失



所以我在Youtube上关注生存射击游戏,即使在我已经完成了这个部分之后,我也遇到了一些令人讨厌的神奇故障。在我制作Slider的那天,它运行得很好。现在,即使值为100,填充也会不断消失。此外,他们用于初始化SliderImage的代码现在不起作用,所以我寻找了一个不同的代码,它现在起作用了。我使用的是2019.3.0f6版本。这是我的密码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour{
int initHp = 100;
public int currentHp;
public Slider hpSlider;
public Image dmgImg;
AudioClip deathClip;
float flashSpd = 5f;
Color flashColor = new Color(1f, 0f, 0f, 0.1f);
Animator animator;
AudioSource playerAudio;
PlayerMovement playerMovement;
PlayerShooting playerShooting;
bool isDead;
bool isDamaged;
void Awake() {
animator = GetComponent<Animator>();
playerAudio = GetComponent<AudioSource>();
playerMovement = GetComponent<PlayerMovement>();
playerShooting = GetComponentInChildren<PlayerShooting>();
currentHp = initHp;
}
void Start(){
if (GameObject.FindGameObjectWithTag ("hpSlider")) {
hpSlider = (Slider) FindObjectOfType(typeof (Slider));
}
if (GameObject.FindGameObjectWithTag ("dmgImg")) {
dmgImg = (Image) FindObjectOfType(typeof (Image));
}
}
// Update is called once per frame
void Update(){
if(dmgImg != null){
if(isDamaged){
dmgImg.color = flashColor;
Debug.Log("flash color");
}else{
dmgImg.color = Color.Lerp(dmgImg.color, Color.clear, flashSpd * Time.deltaTime);
}
}
dmgImg.color = Color.Lerp(dmgImg.color, Color.clear, flashSpd * Time.deltaTime);
isDamaged = false;
Debug.Log("currentHp: "+currentHp+" sliderVal: "+hpSlider.value);
}
public void TakeDamage(int amt){
isDamaged = true;
currentHp -= amt;
// hpSlider.value = currentHp;
Debug.Log("currentHp: "+currentHp+" sliderVal: "+hpSlider.value);
playerAudio.Play();
if(currentHp <= 0 && !isDead){
Death();    
}
}
public void Death(){
isDead = true;
animator.SetTrigger("Die");
// playerAudio.clip =   deathClip;
// playerAudio.Play();
playerMovement.enabled = false;
playerShooting.enabled = false;
}
}

您可以使用[Range()]属性,它的工作原理如下:

[Range(0, 100)] public int currentHp

很抱歉没有评论,因为我认为这不是一个答案,但我的代表太低了(:

最新更新