假设脚本在地图上找到两个对象之间的中心点。
public List<Transform> targets;
Vector3 GetCenterPoint()
{
if (targets.Count == 1)
{
return targets[0].position;
}
var bounds = new Bounds(targets[0].position, Vector3.zero);
for (int i=0; i < targets.Count; i++)
{
bounds.Encapsulate(targets[i].position);
}
return bounds.center;
}
但是如果它们只是出现在地图上,如何找到它们之间的中心呢?如何在地图上创建预制件:
if (!spawned)
{
if (Input.GetKey(KeyCode.Q))
{
Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
spawned = true;
}
}
显然,您需要在创建新对象时将其添加到工作表中
targets。你可以这样做:因此,在您上面编写的方法中获取中心点时,将考虑所有生成的对象。