第一次在這里發帖,如果有什么不對的地方請見諒。我寫了一大段代碼來檢查附加的物體是否接觸到另一個帶有“危險”標簽的物體。如果是,則浮點“hp”的值將減 1。當我運行我的游戲時沒有錯誤訊息出現,但是當我運行我的游戲并用我的玩家角色觸摸一個標記為“hazard”的物件時,hp 不是影響 - 我知道這一點,因為我成功實作了一個健康欄,當我用標簽觸摸物件時它根本不會改變。
下面是我撰寫的用于檢測碰撞并降低 hp 值的代碼。
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag =="hazard")
{
hp = hp - 1;
}
}
玩家角色物件和危險物件都附加了 2D Collider 組件。
根據要求,健康代碼已添加如下:
這段代碼設定了“hp”的初始值,并宣告了我稍后用來表示玩家剩余生命值的游戲物件。
float hp = 10;
public GameObject phealth1;
public GameObject phealth2;
public GameObject phealth3;
public GameObject phealth4;
public GameObject phealth5;
public GameObject phealth6;
public GameObject phealth7;
public GameObject phealth8;
public GameObject phealth9;
public GameObject phealth10;
此代碼防止“hp”高于或低于最大值和最小值,以及根據“hp”的值更新健康欄。
void Update()
{
if (hp > 10)
hp = 10;
if (hp < 0)
hp = 0;
switch (hp) {
case 10:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(true);
phealth10.gameObject.SetActive(true);
break;
case 9:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(true);
phealth10.gameObject.SetActive(false);
break;
case 8:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 7:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 6:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 5:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 4:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 3:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 2:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 1:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(false);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 0:
phealth1.gameObject.SetActive(false);
phealth2.gameObject.SetActive(false);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
}
}
下面是整個腳本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health : MonoBehaviour
{
float hp = 10;
public GameObject phealth1;
public GameObject phealth2;
public GameObject phealth3;
public GameObject phealth4;
public GameObject phealth5;
public GameObject phealth6;
public GameObject phealth7;
public GameObject phealth8;
public GameObject phealth9;
public GameObject phealth10;
// Start is called before the first frame update
void Start()
{
}
void dmg(int x)
{
hp = hp - x;
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "hazard")
dmg(1);
Debug.Log("Contact made");
Debug.Log(hp);
}
// Update is called once per frame
void Update()
{
if (hp > 10)
hp = 10;
if (hp < 0)
hp = 0;
switch (hp) {
case 10:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(true);
phealth10.gameObject.SetActive(true);
break;
case 9:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(true);
phealth10.gameObject.SetActive(false);
break;
case 8:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 7:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 6:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 5:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 4:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 3:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 2:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 1:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(false);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 0:
phealth1.gameObject.SetActive(false);
phealth2.gameObject.SetActive(false);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
}
}
}
uj5u.com熱心網友回復:
我懷疑現場有問題。
- 檢查場景中是否只有一個此腳本。
在
Hierarchy Tab
搜索欄中輸入t:MyComponentName
。使用相同 phealth 物件的多個副本可能會導致您遇到的問題。
- 仔細檢查檢查
phealth{n}
器中分配的游戲物件。
這是您的代碼的清理版本。測驗并確認作業。
using UnityEngine;
public class Health : MonoBehaviour
{
public int health;
public int maxHealth;
public GameObject[] healthObjs;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("hazard"))
{
AddHealth(-1);
}
}
private void AddHealth(int amount)
{
health = Mathf.Max(0, Mathf.Min(maxHealth, health amount));
UpdateDisplayedHealth();
}
private void UpdateDisplayedHealth()
{
for (int i = 0; i < healthObjs.Length; i )
{
healthObjs[i].SetActive(i 1 <= health);
}
}
}
- 洗掉了開關盒,用 for 回圈替換它,這使得代碼更加緊湊。
- 更新了傷害函式以處理最小/最大生命值并更新顯示的生命值。
- 完全洗掉更新。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/461611.html
上一篇:特定邊上垂線上的一點
下一篇:返回列表