【发布时间】:2020-03-11 20:51:50
【问题描述】:
我有一个从玩家发射的射弹预制件,当它与“边界”碰撞时,它应该会自行摧毁,而当它击中“咕噜声”时,它应该会摧毁自己和咕噜声。然而,当它撞到边界时,它会破坏自身和边界的对撞机。我创建了一个自定义标签脚本,允许我将多个标签分配给一个游戏对象,而不仅仅是一个。
为什么它会破坏墙壁对撞机? 为什么它将墙壁和咕噜声都检测为咕噜声? 我将如何解决它?
这是导致问题的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyProjectile : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
//FIX detects only grunt
private void OnTriggerEnter(Collider other)
{
//Get Tag Component
Tags hitObject = other.GetComponent<Tags>();
//Collisions
if (hitObject.FindTag("boundary"))
{
//collision with wall
Debug.Log("Hit a Wall");
Destroy(gameObject);
}
else if (hitObject.FindTag("grunt"))
{
//collision with grunt
Debug.Log("Hit a Grunt");
Destroy(gameObject);
Destroy(other); //<---- Deletes Boundary Collider (Should be destroying the game object of the grunt, instead destroys the colliders of barriers and the grunt)
}
}
}
这是自定义标签脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tags : MonoBehaviour
{
public string[] startTags;
private static string[] tags;
private void Start()
{
tags = startTags;
}
public bool FindTag(string search)
{
bool results = false;
for (int i = 0; i < tags.Length; i++)
{
if(search == tags[i])
{
results = true;
break;
}
}
return results;
}
}
【问题讨论】:
-
使用
CompareTag! -
@derHugo 我认为他们的设计要求游戏对象可以使用一组标签进行标记,而不是一个或零个标签。