首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Vector3转换为Vector3Int时出错

将Vector3转换为Vector3Int时出错
EN

Stack Overflow用户
提问于 2022-11-11 23:12:44
回答 2查看 38关注 0票数 0

我的代码设置如下:

代码语言:javascript
复制
 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()));
            }
        }
    }

我的另一个功能是这样设置的:

代码语言:javascript
复制
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转换为浮动

我想弄清楚为什么我不能把它传递给这个函数。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-11 23:53:31

看看这个文档页面:https://docs.unity3d.com/ScriptReference/Vector3Int-ctor.html

由于您已经在使用Vector3Int,所以使用Mathf.FloorToInt也是不可能的。我不明白您想在那里做什么,但是如果您只是想创建一个Vector3Int,就按文档中的话做。

代码语言:javascript
复制
Vector3Int m_Position = new Vector3Int(1, 5, -2);
票数 1
EN

Stack Overflow用户

发布于 2022-11-11 23:17:39

伙计,Mathf.FloorToInt只能在浮点上使用,不能在向量上使用

https://docs.unity3d.com/ScriptReference/Mathf.FloorToInt.html

你是在一个很大程度上是向量的位置通过的。

遇到麻烦时总是去看医生。

非常奇怪,你使用的是"Int“版本的Vector3。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74409063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档