我的代码设置如下:
private void TrySpawningAnAgent(GameObject startStructure, GameObject endStructure){
if (startStructure != null && endStructure != null){
houses=GameObject.FindGameObjectsWithTag("Houses");
specials = GameObject.FindGameObjectsWithTag("Special");
Vector3Int startPosition = Mathf.FloorToInt(houses[UnityEngine.Random.Range(0, houses.Length)].transform.position);
Vector3Int endPosition = Mathf.FloorToInt(specials[UnityEngine.Random.Range(0, specials.Length)].transform.position);
var agent = Instantiate(GetRandomPedestrian(), startPosition, Quaternion.identity);
var path = placementManager.GetPathBetween(startPosition, endPosition);
if (path.Count > 0)
{
path.Reverse();
var aiAgent = agent.GetComponent<AIAgent>();
aiAgent.Initialize(new List<Vector3>(path.Select(x => (Vector3)x).ToList()));
}
}
}我的另一个功能是这样设置的:
internal List<Vector3Int> GetPathBetween(Vector3Int startPosition, Vector3Int endPosition)
{
var resultPath = GridSearch.AStarSearch(placementGrid, new Point(startPosition.x, startPosition.z), new Point(endPosition.x, endPosition.z));
List<Vector3Int> path = new List<Vector3Int>();
foreach (Point point in resultPath)
{
path.Add(new Vector3Int(point.X, 0, point.Y));
}
return path;
}但是,在运行FloorToInt时,我仍然会得到相同的错误。无法从UnityEngine.Vector3转换为浮动
我想弄清楚为什么我不能把它传递给这个函数。
发布于 2022-11-11 23:53:31
看看这个文档页面:https://docs.unity3d.com/ScriptReference/Vector3Int-ctor.html
由于您已经在使用Vector3Int,所以使用Mathf.FloorToInt也是不可能的。我不明白您想在那里做什么,但是如果您只是想创建一个Vector3Int,就按文档中的话做。
Vector3Int m_Position = new Vector3Int(1, 5, -2);发布于 2022-11-11 23:17:39
伙计,Mathf.FloorToInt只能在浮点上使用,不能在向量上使用。
https://docs.unity3d.com/ScriptReference/Mathf.FloorToInt.html
你是在一个很大程度上是向量的位置通过的。
遇到麻烦时总是去看医生。
非常奇怪,你使用的是"Int“版本的Vector3。
https://stackoverflow.com/questions/74409063
复制相似问题