[英]Unity not destroying ontriggerenter2d object with TypeFruit
I am new to Unity and c# and have been learning for a few months now. 我是Unity和C#的新手,已经学习了几个月。 I have a problem with a project I am working on.
我正在处理的项目有问题。 In the game, a player runs around picking fruit from trees.
在游戏中,玩家跑来跑去从树上采摘水果。 I want when the player is colliding an object with the tag "TypeFruit", the fruit is destroyed.
我想要当播放器将带有“ TypeFruit”标签的对象与水果碰撞时将其破坏。 The Fruit is not getting picked when the player touches it and I don't know why.
当玩家触摸水果时,水果并没有被拾取,我也不知道为什么。 Could somebody share there knowledge about this?
有人可以分享有关这方面的知识吗? There are no error messages showing.
没有错误消息显示。 Thank You.
谢谢。
I have tried this. 我已经试过了。
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "TypeFruit")
{
Destroy(collision.gameObject);
}
}
This is the Player script 这是播放器脚本
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);
}
}
}
And this is the Fruit script 这是水果剧本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fruit : MonoBehaviour
{
int Healthiness;
void Start()
{
}
}
And this is the treeplacer script. 这是treeplacer脚本。 The treeplacer spawns trees randomly across the map.
树形放置器在地图上随机生成树木。 Each tree grows fruit.
每棵树都长出果实。 I want the player to pick the fruit if he touches it.
我希望玩家触摸它就能摘下水果。 Fruits are tagged with the tag "TypeFruit".
水果被标记为“ 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
}
No errors at all. 完全没有错误。 Trees spawn perfectly fine.
树木产生得很好。 Fruits are also spawning fine but are not getting picked when the player touches it.
水果也可以很好地产卵,但是当玩家触摸它时就不会被采摘。
它可能是以下内容之一。1.标签名称中有拼写错误。2.对象之一没有2dcollider或刚性主体2d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.