У меня есть панель хитов, которая должна заполняться на 0,1 каждый раз, когда объект ball сталкивается с объектом goodOrb. Однако бар заполняется на 0,1 только при первом столкновении. Он не двигается при повторном столкновении мяча.
Я попробовал Debug. Записать значение переменной 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);
}
}
Я ожидаю, что увеличение будет добавляться к хит-точкам каждый раз, когда происходит столкновение и заполняется панель хит-точек.