我是新来的,不确定这是不是一个愚蠢的问题。
我想让某些东西查看最近的带有特定标签的对象。
我试过这个代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TowerScript : MonoBehaviour
{
public GameObject.Enemy
// Update is called once per frame
Void; Update
transform.LookAt(GameObject.Enemy);
}
}
您可以使用此代码查找对象
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Enemy");
,你可以用这个方法找到最近的对象
Transform GetClosestEnemy(Transform[] enemies)
{
Transform tMin = null;
float minDist = Mathf.Infinity;
Vector3 currentPos = transform.position;
foreach (Transform t in enemies)
{
float dist = Vector3.Distance(t.position, currentPos);
if (dist < minDist)
{
tMin = t;
minDist = dist;
}
}
return tMin;
}