gpt4 book ai didi

c# - Projectile 将墙壁和 "grunt"检测为 "grunt"

转载 作者:行者123 更新时间:2023-11-30 22:51:44 26 4
gpt4 key购买 nike

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

为什么它会破坏墙壁碰撞器?为什么它将墙壁和咕噜声都检测为咕噜声?我将如何修复它?

这是导致问题的脚本:

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;
}
}

Enemy Grunt Inspector

Wall Inspector

Projectile Inspector

最佳答案

看来你有两个问题。

1) 首先是你的抛射物正在摧毁 grunt 的对撞机,而不是 grunt。这是因为您将对撞机传递给 destroy 函数,而不是 grunt:

Destroy(other);

改为使用

Destroy(other.gameobject);

我也会把这个放在

Destroy(gameObject);

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

private static string[] tags;

是静态的。我会删除 static 修饰符,看看它是否有效。您也可以直接使用 startTags,无需 start() 赋值。

Unity 也已经有了一个可能更适合这种检测的标签系统: https://docs.unity3d.com/Manual/Tags.html

关于c# - Projectile 将墙壁和 "grunt"检测为 "grunt",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58881471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com