我正在制作 3d 塔防游戏,并且我有一个 10x10 的图块,路径将位于该图块上。我目前正在尝试做到这一点,以便当您单击时,游戏会检测到其周围的图块以防止非法路径。然而,它正在检测自己。我该如何解决这个问题?
基本上,我不希望每个图块都检测到自己。代码如下。
路径创建器.cs
using System.Collections;
using System.Collections.Generic;
using Unity.Android.Types;
using UnityEngine;
public class PathCreator : MonoBehaviour
{
public Mesh StraightPath;
public Mesh TurnPath;
public float radius = 5f;
public static string targetTag = "StraightPath";
public static bool Message = false;
public static bool Message2 = false;
public int pathLayer;
public Collider myCollider;
// Start is called before the first frame update
void Start()
{
pathLayer = 7 << LayerMask.NameToLayer("Tile");
myCollider = GetComponent<Collider>();
}
// Update is called once per frame
void Update()
{
Collider[] hitColliders = Physics.OverlapSphere(transform.position, radius, pathLayer);
foreach (var hitCollider in hitColliders)
{
if (hitCollider != myCollider)
{
if (hitCollider.gameObject.CompareTag(targetTag))
{
if (!Message2)
{
Message2 = true;
print("Found Me!");
}
}
}
else
{
if (!Message)
{
print("Found path!");
Message = true;
}
}
}
}
private void OnMouseDown()
{
print("click!");
gameObject.GetComponent<MeshFilter>().mesh = StraightPath;
gameObject.tag = "StraightPath";
Message = false;
Message2 = false;
}
}
PathSelect.cs(当前未使用)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathSelect : MonoBehaviour
{
public GameObject StraightPath;
public GameObject TurnPath;
public Camera PathChooser;
public Camera MainCamera;
// Start is called before the first frame update
void Start()
{
PathChooser.enabled = true;
MainCamera.enabled = false;
}
// Update is called once per frame
void Update()
{
}
}
这是我当前的代码,我有这些图块。我无法在瓷砖上放置一层,因为它们都需要检测路径何时变成直线路径(参见第二张图片)
我想阻止^^这种情况发生。
我该如何解决这个问题?
您可以跳过正在处理的调用对象,如下所示:
Collider[] hitColliders = Physics.OverlapSphere(transform.position, radius, pathLayer);
foreach (var hitCollider in hitColliders) {
if (hitCollider.gameObject == gameObject)
continue;
// Continue processing stuff
}