我有一个击点栏,每次球对象与goodOrb对象碰撞时,该点都应填充0.1。但是,只有在第一次碰撞时,横条才会填充0.1。当球再次碰撞时,它不会移动。
我尝试了Debug.Log,它记录了变量hitPoints的值,该值存储了填充条的当前数量。此变量初始化为0,每当两个对象发生碰撞时,我都应该将另一个变量称为增加,将其设置为0.1,将增加变量添加到生命值。但这仅发生一次。 Debug.Log(hitpoints)仅显示一次0.1。
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);
}
}
我希望每次发生碰撞并填充填充点时,都会增加填充量。
1-确保每次球都与相同的球体碰撞。2-确保一个对撞机isTrigger为true。3-确保每次球碰撞时调用触发功能。通过做这个void OnTriggerEnter2D(Collider2D other){
Debug.Log (other.tag);
}