c# - 使积分收集并转到其他地方的脚本,使用“itrigger”标签影响所有内容
问题描述
我遇到了一些麻烦,每次我用带有触发器的对撞机撞到任何东西时,它都会产生一个点并删除具有触发器的对象,我正在考虑向可收集点添加一个图层或标签该脚本将检测并仅影响带有标签或图层的对象,但无法弄清楚这一点。
这是我的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collect : MonoBehaviour
{
private Rigidbody2D rb;
[SerializeField]
private GameObject Point;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
SpawnPoint();
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
}
private void SpawnPoint()
{
bool pointSpawned = false;
while (!pointSpawned)
{
Vector3 pointPosition = new Vector3(Random.Range(-13f, 13f), Random.Range(-8f, 8f), 0f);
if ((pointPosition - transform.position).magnitude < 3)
{
continue;
}
else
{
Instantiate(Point, pointPosition, Quaternion.identity);
pointSpawned = true;
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
Destroy(collision.gameObject);
SpawnPoint();
}
}
解决方案
您可以使用该CompareTag
功能检查对象是否属于某个标签。
试试这个:
OnEnterTrigger2D(Collider2D collision)
{
if(collision.collider.gameobject.CompareTag("The tag you want to compare"))
{
Destroy(collision.gameObject);
SpawnPoint();
}
}
推荐阅读
- c - C 找不到 sqrt{2} 的第 10 位十进制数字
- python - 迭代所有 Dataframe 行并执行startswith()
- javascript - Nuxt/Vue-Component 未监听事件
- ruby-on-rails - 表单无法定向到编辑路径
- node.js - 如何在mongodb中查询嵌套对象
- android - 布局 XML 文件中的 android:onClick 标记中的错字未显示为错误。构建完成没有警告
- python - 初始化 python 交互式绘图以提高响应能力
- nginx - 如何在 Kubernetes nginx 控制器中暴露 80 端口
- windows - 如何在 Windows PC 中分别创建多个 Flutter 版本别名以在命令提示符中使用
- api - 将事件从 API 显示到 Flutter Carousel Calender