- Home /
The question is answered, right answer was accepted
What the hell? Cannot convert float to int PROBLEM.
Why is this not working?
This is basically trying to get the heights of a certain point in the terrain, but that last part ("Get heights") is causing a cannot convert to int issue.
Full error " error CS1503: Argument #1' cannot convert
float' expression to type `int'"
Its basically the positions bPosX1 and bPosY1... it simply wont work if I put them into the get heights. Without the getheights code everything works fine. So the convertion part of bpoX01 and bposY01 seems to be fine.
float bposX01 = bZone1.transform.position.x;
float bposZ01 = bZone1.transform.position.z;
float bposY01 = GameObject.Find("Terrain 0 3").GetComponent<Terrain>().SampleHeight(new Vector3(bposX01, 0, bposZ01));
Vector3 nPos = (transform.position - terrainObject.gameObject.transform.position);
Vector3 crd;
crd.x = nPos.x / ter.terrainData.size.x;
crd.y = nPos.y / ter.terrainData.size.y;
crd.z = nPos.z / ter.terrainData.size.z;
bposX01 = (int) (crd.x * terrain.terrainData.heightmapWidth);
bposY01 = (int) (crd.z * terrain.terrainData.heightmapHeight);
//Offset...
int ost = Inst1._size / 2;
//Get heights.
float[,] hPoints = ter.terrainData.GetHeights(bposX01-ost, bposY01-ost, size, size);
Please help... thanks.
Huh I seemed to have fixed it by adding this to the problem line.
float[,] hPoints = ter.terrainData.GetHeights((int)bposX01-ost, (int)bposY01-ost, size, size);
I wont accept as answer because I'm not sure this is the correct way to solve it.
Is it?
... I'm scared.
Answer by fafase · Apr 12, 2015 at 09:02 AM
float[,] hPoints = ter.terrainData.GetHeights(
(int)bposX01-ost, (int)bposY01-ost, size, size);
Answer by Addyarb · Apr 12, 2015 at 09:01 AM
Replace line 30 with
float[,] hPoints = ter.terrainData.GetHeights((float)bposX01-ost, (float)bposY01-ost, size, size);
That should work. You are defining bposX01 and bposY01 as integers above, and calling them as floats later. You must do a conversion or else you'll usually get that error.
Follow this Question
Related Questions
transform.position as variable to lock object 0 Answers
seperation of prefabs with two dimensional terrain generation 0 Answers
transform.position = new Vector 3 NOT moving to correct position? 1 Answer
Convert terrain map coordinates (GetDetailLayer) into world position 0 Answers
Accurate placed object's transform messes up on game start 1 Answer