【问题标题】:Unity not destroying ontriggerenter2d object with TypeFruitUnity 不使用 TypeFruit 破坏 ontriggerenter2d 对象
【发布时间】:2019-08-31 20:56:41
【问题描述】:

我是 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 脚本。 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
}

完全没有错误。树木生成得非常好。水果也能正常生成,但玩家触摸时不会被采摘。

【问题讨论】:

  • 你有一个 Rigidbody2D 附加到水果上吗?不幸的是,这是 onTriggerEnter2D 工作所必需的。
  • 我之前修复了这个错误。你不是水果上的刚体。无论如何,谢谢你提到它! :)

标签: c# unity3d


【解决方案1】:

它可能是其中之一 1.标签名称中的错字 2.其中一个物体没有2dcollider或rigidbody2d

【讨论】:

  • 我以前修过这个。谢谢你。问题是 2。无论如何感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-28
  • 2019-05-27
  • 2015-02-14
  • 2020-08-19
  • 1970-01-01
相关资源
最近更新 更多