C# 生命值条不会完全填充,计算只发生一次
我有一个生命点栏,它应该在每次球对象与goodOrb对象碰撞时填充0.1。但是,只有在第一次碰撞时,条形图才会填充0.1。当球再次碰撞时,它不会移动 我试过调试。记录变量hitPoints的值,它存储了当前填充条的数量。这个变量被初始化为0,我有另一个变量叫做increase,它被设置为0.1,每次两个对象碰撞时,变量increase应该被添加到hitpoints。但这只会发生一次。Log(hitpoints)仅显示一次0.1C# 生命值条不会完全填充,计算只发生一次,c#,unity3d,game-development,C#,Unity3d,Game Development,我有一个生命点栏,它应该在每次球对象与goodOrb对象碰撞时填充0.1。但是,只有在第一次碰撞时,条形图才会填充0.1。当球再次碰撞时,它不会移动 我试过调试。记录变量hitPoints的值,它存储了当前填充条的数量。这个变量被初始化为0,我有另一个变量叫做increase,它被设置为0.1,每次两个对象碰撞时,变量increase应该被添加到hitpoints。但这只会发生一次。Log(hitpoints)仅显示一次0.1 using System.Collections; using Sy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class goodOrb : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public Image barImage;
private Rigidbody2D rb;
public float increase = 0.1f; //amount to increase bar each time a collision happens
public float hitPoints = 0.0f; // current amount the bar is filled by
// public AudioSource collisionSound;
int scoreValue= 5 ;
// Start is called before the first frame update
public void Start()
{
GameObject Filler = GameObject.Find("Filler");
Image barImage = Filler.GetComponent<Image>();
}
void OnTriggerEnter2D(Collider2D other)
{
ParticleSystem ps = GetComponent<ParticleSystem>();
if(other.tag=="Ball")
{
ps.Play();
HitPoints();
scoreManager.score += scoreValue;
// barImage.fillAmount = (float)currentHealth / (float)maxHealth;
// collisionSound.Play();
}
}
void Update()
{
}
// Update is called once per frame
void HitPoints()
{
GameObject Filler = GameObject.Find("Filler");
Image barImage = Filler.GetComponent<Image>();
hitPoints = hitPoints + increase;
barImage.fillAmount = hitPoints;
//print(hitPoints);
Debug.Log(hitPoints);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类goodOrb:单一行为
{
公共int maxHealth=100;
公共卫生;
公众形象;
私有刚体2d rb;
公共浮动增加=0.1f;//每次发生碰撞时增加条的量
public float hitPoints=0.0f;//条的当前填充量
//公共声源碰撞声;
积分值=5;
//在第一帧更新之前调用Start
公开作废开始()
{
GameObject Filler=GameObject.Find(“Filler”);
Image barImage=Filler.GetComponent();
}
无效OnTiggerEnter2D(碰撞的R2D其他)
{
ParticleSystem ps=GetComponent();
if(other.tag==“Ball”)
{
ps.Play();
致命点();
scoreManager.score+=scoreValue;
//barImage.fillAmount=(float)currentHealth/(float)maxHealth;
//碰撞声。播放();
}
}
无效更新()
{
}
//每帧调用一次更新
无效生命值()
{
GameObject Filler=GameObject.Find(“Filler”);
Image barImage=Filler.GetComponent();
生命值=生命值+增加;
barImage.fillAmount=生命值;
//打印(命中率);
Debug.Log(hitPoints);
}
}
我希望每次碰撞发生时,生命值都会增加,并且生命值栏会填满。1-确保每次球都与同一个球体对象碰撞。 2-确保一个对撞机isTrigger为真。 3-确保每次球碰撞时都调用触发函数。 通过这样做
void OnTriggerInter2d(碰撞的R2D其他){
Debug.Log(other.tag);
}
听起来这个脚本只在一个项目上。。也许还有其他原因让它不能做得更多,但是从这一点很难判断脚本是否在游戏对象goodOrb上,它不需要在填充条上当球与goodOrb碰撞时,0.1f应该被添加到生命值变量中球体对象是随机产生的,其中很多是的,每次都会调用函数通过这样做增加填充条的数量…`barImage.fillAmount=barImage.fillAmount+0.1f`