我的代碼設定如下:
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 轉換為 Float
我試圖弄清楚為什么我不能通過函式傳遞它。
uj5u.com熱心網友回復:
看看這個檔案頁面:https ://docs.unity3d.com/ScriptReference/Vector3Int-ctor.html
由于您已經在使用Vector3Int
,因此也沒有必要使用 Mathf.FloorToInt 也是不可能的。我不明白你想在那里做什么,但如果你只是想創建一個 Vector3Int,就按照檔案中的說明去做。
Vector3Int m_Position = new Vector3Int(1, 5, -2);
uj5u.com熱心網友回復:
伙計,Mathf.FloorToInt 僅在 FLOAT 上可用,在向量上不可用。
https://docs.unity3d.com/ScriptReference/Mathf.FloorToInt.html
您正在傳遞一個 POSITION,它在很大程度上是一個向量。
遇到問題時總是去看檔案。
順便說一句,您使用的是 Vector3 的“Int”版本,這很奇怪。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/533277.html