Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 射弹同时探测墙壁和;“咕噜”;作为一个「;“咕噜”;_C#_Unity3d - Fatal编程技术网

C# 射弹同时探测墙壁和;“咕噜”;作为一个「;“咕噜”;

C# 射弹同时探测墙壁和;“咕噜”;作为一个「;“咕噜”;,c#,unity3d,C#,Unity3d,我有一个从玩家身上发射的投射物预制,当它与“边界”碰撞时,它应该摧毁自己,当它击中“咕噜声”时,它应该摧毁自己和咕噜声。然而,当它撞击边界时,它会破坏自身和边界的碰撞器。我创建了一个自定义标记脚本,允许我将多个标记分配给游戏对象,而不是只分配一个 为什么它要摧毁墙对撞机? 为什么它会将墙壁和呼噜声都检测为呼噜声? 我该如何着手修理它 以下是导致问题的脚本: using System.Collections; using System.Collections.Generic; using Unit

我有一个从玩家身上发射的投射物预制,当它与“边界”碰撞时,它应该摧毁自己,当它击中“咕噜声”时,它应该摧毁自己和咕噜声。然而,当它撞击边界时,它会破坏自身和边界的碰撞器。我创建了一个自定义标记脚本,允许我将多个标记分配给游戏对象,而不是只分配一个

为什么它要摧毁墙对撞机? 为什么它会将墙壁和呼噜声都检测为呼噜声? 我该如何着手修理它

以下是导致问题的脚本:

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)
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类:单一行为
{
//在第一帧更新之前调用Start
void Start()
{
}
//每帧调用一次更新
无效更新()
{
}
//修正只检测咕噜声
专用空对撞机(对撞机其他)
{
//获取标记组件
标记hitObject=other.GetComponent();
//碰撞
if(hitObject.FindTag(“边界”))
{
//撞墙
Log(“撞墙”);
摧毁(游戏对象);
}
else if(hitObject.FindTag(“grunt”))
{
//与咕噜声碰撞
Log(“发出咕噜声”);
摧毁(游戏对象);

摧毁(其他);//看来你有两个问题

1)首先,您的射弹正在摧毁呼噜声的对撞机,而不是呼噜声。这是因为您将对撞机传递给销毁功能,而不是呼噜声:

Destroy(other);
改用

Destroy(other.gameobject);
我也会把这个放在

Destroy(gameObject);
2)您的标签系统不工作。我猜这是因为

private static string[] tags;
是静态的。我会删除静态修饰符,看看它是否有效。您也可以直接使用
startTags
,不需要
start()
赋值

Unity还有一个标签系统,可能更适合这种检测:

Use!@derHugo我认为他们的设计要求游戏对象可以使用一组标签,而不是一个或零个标签。
private static string[] tags;