Home > Blockchain >  Error when converting Vector3 to Vector3Int
Error when converting Vector3 to Vector3Int

Time:11-12

My code is setup like this:

 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()));
            }
        }
    }

My other function is set up like this:

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;
    }

However I keep getting the same error when running FloorToInt. cannot convert from UnityEngine.Vector3 to Float

I'm trying to figure out why I can't pass it through the function.

CodePudding user response:

Dude, Mathf.FloorToInt is only available ON A FLOAT, not on a vector.

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

You are passing in a POSITION which is very much a vector.

Always go to the docs when having trouble.

Just BTW it's very strange you're using the "Int" version of Vector3.

CodePudding user response:

Take a look at this doc page: https://docs.unity3d.com/ScriptReference/Vector3Int-ctor.html

As you are already using Vector3Int there is no point of using Mathf.FloorToInt as well is not possible. I don't understand what you are trying to do there, but if you are just trying to create a Vector3Int just do as it says in the docs.

Vector3Int m_Position = new Vector3Int(1, 5, -2);
  • Related