簡體   English   中英

Unity不使用TypeFruit銷毀ontriggerenter2d對象

[英]Unity not destroying ontriggerenter2d object with TypeFruit

我是Unity和C#的新手,已經學習了幾個月。 我正在處理的項目有問題。 在游戲中,玩家跑來跑去從樹上采摘水果。 我想要當播放器將帶有“ TypeFruit”標簽的對象與水果碰撞時將其破壞。 當玩家觸摸水果時,水果並沒有被拾取,我也不知道為什么。 有人可以分享有關這方面的知識嗎? 沒有錯誤消息顯示。 謝謝。

我已經試過了。

void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "TypeFruit")
        {
            Destroy(collision.gameObject);
        }
    }

這是播放器腳本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player_movement : MonoBehaviour
{

    int speed=2;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("w"))
        {
            transform.Translate(0, 0.05f * speed, 0);

        }
        if (Input.GetKey("s"))
        {
            transform.Translate(0, -0.05f * speed, 0);


        }
        if (Input.GetKey("a"))
        {
            transform.Translate(-0.05f * speed, 0, 0);
        }
        if (Input.GetKey("d"))
        {
            transform.Translate(0.05f * speed, 0, 0);
        }
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "TypeFruit")
        {
            Destroy(collision.gameObject);
        }
    }

}

這是水果劇本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fruit : MonoBehaviour
{
    int Healthiness;



    void Start()
    {




    }

}

這是treeplacer腳本。 樹形放置器在地圖上隨機生成樹木。 每棵樹都長出果實。 我希望玩家觸摸它就能摘下水果。 水果被標記為“ TypeFruit”。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TreePlacer : MonoBehaviour
{
    public Sprite[] FruitSprites = new Sprite[3];
    public Fruit FruitPrefab;

    public GameObject TreePrefab;
    // Start is called before the first frame update
    void Start()
    {
        int TreeCount = Random.Range(8, 16);
        for(int i=1; i<=TreeCount; i++)
        {
          GameObject CurrentTree= Instantiate(TreePrefab, new Vector3(Random.Range(-5, 6), Random.Range(-5, 6), 0), Quaternion.identity);
            CurrentTree.AddComponent(typeof(Tree));
            CurrentTree.GetComponent<Tree>().FruitPrefab = FruitPrefab.gameObject;
            CurrentTree.GetComponent<Tree>().myTreePlacer = this;
        }
    }

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

    }

    void PlaceTree()
    {

    }

}

public class Tree:MonoBehaviour
{

    public TreePlacer myTreePlacer;
    public GameObject FruitPrefab;
    public int maxNumberOfFruits;
    public int regrowTime;
    public int climbTime;
    int currentNumberOfFruits=0;
    FruitType MyFruitType;




    void Start()
    {

        MyFruitType = (FruitType)Random.Range(0, 3);

        switch (MyFruitType)
        {
            case FruitType.apple:
                maxNumberOfFruits = 5;
                regrowTime = 20;
                climbTime = 5;

                break;

            case FruitType.banana:

                    maxNumberOfFruits = 5;
                        regrowTime = 30;
                    climbTime = 10;
                    break;

            case FruitType.coconut:
                maxNumberOfFruits = 3;
                regrowTime = 60;
                climbTime = 30;
                break;



        }

        StartCoroutine(GrowFruit());




    }

    IEnumerator GrowFruit()
    {

        while (true)
        {

            yield return new WaitForSeconds(regrowTime);

            Fruit CurrentFruit = Instantiate(FruitPrefab, new Vector3(transform.position.x + Random.Range(-0.5f, 0.5f), transform.position.y + Random.Range(-0.5f, 0.5f)), Quaternion.identity).GetComponent<Fruit>();
            CurrentFruit.GetComponent<SpriteRenderer>().sprite = myTreePlacer.FruitSprites[(int)MyFruitType];
            currentNumberOfFruits++;
            yield return new WaitWhile(() => currentNumberOfFruits >= maxNumberOfFruits);
        }
    }



}



enum FruitType
{
    apple, banana, coconut
}

完全沒有錯誤。 樹木產生得很好。 水果也可以很好地產卵,但是當玩家觸摸它時就不會被采摘。

它可能是以下內容之一。1.標簽名稱中有拼寫錯誤。2.對象之一沒有2dcollider或剛性主體2d

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM