iT邦幫忙

0

unity 2D collider OnTriggerEnter2D的判定問題

大家好,小弟我目前自學Unity來做2d遊戲,有個問題想請教各位大大,請問圖片中角色的collider在OnTriggerEnter2D方法中能做到不讓多個collider觸發嗎?
https://ithelp.ithome.com.tw/upload/images/20200502/20126797zhmREYYGCL.png
https://ithelp.ithome.com.tw/upload/images/20200502/20126797RNF8dLTel6.png
https://ithelp.ithome.com.tw/upload/images/20200502/20126797BG5CxsEslI.png

目前的問題就是我的角色有兩個collider(因為animation的需要),在讓它碰物件的時候下方collider重疊的部份會觸發方法兩次,導致我的物件搜集數量變成-1而不是0(搜集到一個就-1)(如圖所示)

這是要搜集的物件的C#script
public class GoldenRing : MonoBehaviour
{
public int GoldenRingBalue;
public Text text;
public GameObject door;
// Start is called before the first frame update
void Start()
{
text.text = "X" + GoldenRingBalue;
}

// Update is called once per frame
void Update()
{

}
void OnTriggerEnter2D(Collider2D other)
{
    
    if (other.tag == "Player")
    {
        this.gameObject.SetActive(false);
    }
    if (this.gameObject.activeSelf == false)
    {
        GoldenRingBalue -= 1;
        text.text = "X" + GoldenRingBalue;
    }
    if (GoldenRingBalue == 0)
    {
        door.SetActive(true);//門出現
    }
}

}

不知道是否有辦法解決,感謝各位!!!!

dragonH iT邦超人 5 級 ‧ 2020-05-04 10:33:50 檢舉
我覺得你的 code 好像怪怪的
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
eddy1999
iT邦見習生 ‧ 2020-06-04 00:41:40

https://forum.gamer.com.tw/C.php?bsn=60602&snA=3157
我也有這問題,但不知道跟你一不一樣,我也沒去解決,畢竟新手,不太好搞,你看看上面的網址是不是你要的吧!

0
ormaster
iT邦見習生 ‧ 2022-04-09 12:38:10

比較建議分開管理Collider,例如,其中一個腳本負責偵測角色收集物件用Collider,另一個腳本掛動畫需要的Collider,並且這兩個物件變成角色的子物件,由角色來管理

我要發表回答

立即登入回答