1 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個贊
OnCollisionEnter2D旨在僅觸發(fā)發(fā)生相同碰撞的第一幀。
您想使用OnCollisionStay2D(),它會在每一幀發(fā)生碰撞時觸發(fā)。
此外,擺脫while循環(huán)。OnCollisionExit2D在方法終止之前,游戲無法更新(包括調(diào)用)。
private void OnCollisionStay2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy01")
{
GetComponent<AudioSource>().Play();
// To take 5 - 30 damage per second:
HPBAR.PlayHP -= Random.Range(5f,30f) * Time.deltaTime;
if (HPBAR.PlayHP < 0)// Insures player HP bar doesn't read less than zero in GUI TXT
{
HPBAR.PlayHP = 0;
}
// Debug.Log("Ouch! " + HPBAR.PlayHP + " Left");
if (HPBAR.PlayHP <= 0 && HPBAR.lives > 0)
{
HPBAR.lives--;
HPBAR.LPfloat--;
p5.transform.position = spawner.transform.position;
HPBAR.PlayHP = Random.Range(100, 150);
// Debug.Log("You're Dead!!! Lives left: " + (HPBAR.lives + 1));
}
else if (HPBAR.PlayHP <= 0 && HPBAR.lives <= 0)
{
SceneManager.LoadScene("DeathScene");
}
} // of player tag check
}// end of collision stay
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy01")
{
// put "i quit touching the hurty thing" stuff here.
}
}
- 1 回答
- 0 關(guān)注
- 305 瀏覽
添加回答
舉報