Unity不使用TypeFruit销毁ontriggerenter2d对象

时间:2019-08-31 20:56:41

标签: c# unity3d

我是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);
        }
    }

}

这是Fruit脚本

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 个答案:

答案 0 :(得分:0)

可能是其中之一 1.标签名称中的错字 2.其中一个对象没有2dcollider或body2d