- Home /
Tilemap with floats beginner
Hey guys,
i am tryn to make a Tilebased game since i am still a beginner i need some tutorials to help myself with that but i dont wanna copy paste everything so i just use the tutorials as helper while i do my own stuff the tutorial creates a normal tile map while i create a hexdiamonsonal Grid and for that to work properly i seem to need to use floats instead of integer
the multidiamonsenal array graph seems to expect integer while i feed it with floats but i am utterly stuck right now by not knowing where and how to fix that and i also dont know how to google that since all i know is that it somehow expects integer while i have floats the yt tutorial uses integers only so it never really talks about the issue i have run into
basicly everywhere where graph has something to do with i get "Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)"
ClickableTile.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ClickableTile : MonoBehaviour { public float TileX; public float TileZ; public HexGen map;
void OnMouseUp()
{
map.GeneratePathTo(TileX, TileZ);
}
}
Unit.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Unit : MonoBehaviour { public float tileX; public float tileY; }
HexGen.cs
using UnityEngine; using System.Collections.Generic; using System.Linq;
public class HexGen : MonoBehaviour {
[SerializeField] float Mapheight = 16.0f;
[SerializeField] float Mapwidth = 24.0f;
private float Tilexoffset = 1.219f;
private float Tilezoffset = 1.06f;
public GameObject Hextileprefab0;
public GameObject SelectedUnit;
List<Node> currentpath = null;
Node[,] graph;
void Start()
{
CreateHexTileMap();
GeneratePathfindingGraph();
}
void CreateHexTileMap()
{
for (float x = 1.0f; x <= Mapwidth; x++)
{
for (float z = 1.0f; z <= Mapheight; z++)
{
GameObject TempGo = (GameObject) Instantiate(Hextileprefab0);
if (z % 2.0f == 0.0f)
{
TempGo.transform.position = new Vector3(x * Tilexoffset, z * Tilezoffset, 0.0f);
}
else
{
TempGo.transform.position = new Vector3(x * Tilexoffset + Tilexoffset / 2.0f, z * Tilezoffset ,0.0f);
}
SetTileinfo(TempGo, x, z);
ClickableTile ClickTile =TempGo.GetComponent<ClickableTile>();
ClickTile.TileX = TempGo.transform.position.x; //get correct position of transform for float
ClickTile.TileZ = TempGo.transform.position.y;
ClickTile.map = this;
}
}
}
void SetTileinfo(GameObject GO, float x, float z) //Go =TempGo refrebce to the gameobject of a Map tile
{
GO.transform.parent = transform;
GO.name = x.ToString() + " width, " + z.ToString() + " height, ";
}
class Node
{
public List<Node> neighbours;
// pass on knowledge of current position to pathfinding
public float x;
public float z;
public float DistanceTo(Node GoalNode)
{
return Vector2.Distance(new Vector2(x,z), new Vector2(GoalNode.x, GoalNode.z));
}
public Node()
{
neighbours = new List<Node>();
}
}
void GeneratePathfindingGraph()
{
graph = new Node[Mapwidth, Mapheight]; // float to int todo this line throws an error
for (float x = 1.0f; x <= Mapwidth; x++)
{
for (float z = 1.0f; z <= Mapheight; z++)
{
// add Tile coords
graph[x, z] = new Node(); // this line throws an error
graph[x, z].x = x; // this line throws an error
graph[x, z].z = z; // this line throws an error
}
}
for (float x = 1.0f; x <= Mapwidth; x++)
{
for (float z = 1.0f; z <= Mapheight; z++)
{
if (x > 1.0f) //when width > 1
{
graph[x, z].neighbours.Add(graph[x-1.0f, z]); //allow movement to neighbour West
if (z > 1.0f) //when also height is bigger 1 allow to move southwest
{
graph[x, z].neighbours.Add(graph[x-1.0f, z-1.0f]);
}
if(z < Mapheight) //when also smaller then mapsize allow to move northwest
{
graph[x, z].neighbours.Add(graph[x-1.0f, z+1.0f]);
}
}
if (x < Mapwidth) //when width < then biggest map width
{
graph[x, z].neighbours.Add(graph[x+1.0f, z]); //allow movement to East
if (z > 1.0f)
{
graph[x, z].neighbours.Add(graph[x, z-1.0f]); //when height bigger then 1 allow movement to southeast
}
if (z < Mapheight)
{
graph[x, z].neighbours.Add(graph[x, z+1.0f]); //when height smaller then biggest mapheight allow movement to north east
}
}
}
}
}
public void GeneratePathTo(float x, float z)
{
currentpath = null;
Dictionary<Node, float> nodetonodedistance = new Dictionary<Node, float>();
Dictionary<Node, Node> previousNode = new Dictionary<Node, Node>();
List<Node> unvisited = new List<Node>();
Node source = graph[SelectedUnit.GetComponent<Unit>().tileX, SelectedUnit.GetComponent<Unit>().tileY];
Node target = graph[x,z];
nodetonodedistance[source] = 0.0f;
previousNode[source] = null;
foreach (Node n in graph)
{
if (n != source)
{
nodetonodedistance[n] = Mathf.Infinity;
previousNode[n] = null;
}
unvisited.Add(n);
}
while (unvisited.Count > 0)
{
//firstnodeofthepath gets updated every tile we move the secound of the closest path becomes the first node after 1 step until the target destination is reached
Node firstnodeofpath = null;
foreach (Node possiblefirstnodeofpath in unvisited)
{
if (firstnodeofpath == null || nodetonodedistance[possiblefirstnodeofpath] < nodetonodedistance[firstnodeofpath])
{
firstnodeofpath = possiblefirstnodeofpath;
}
}
if (firstnodeofpath == target)
{
break;
}
unvisited.Remove(firstnodeofpath);
foreach (Node n in firstnodeofpath.neighbours)
{
float alt = nodetonodedistance[n] + firstnodeofpath.DistanceTo(n);
if (alt < nodetonodedistance[n])
{
nodetonodedistance[n] = alt;
previousNode[n] = firstnodeofpath;
}
}
}
if(previousNode[target] == null)
{
return;
}
currentpath = new List<Node>();
Node curr = target;
while (curr != null)
{
currentpath.Add(curr);
curr = previousNode[curr];
}
currentpath.Reverse();
}
public Vector3 TileCordtoWorldCord(float x, float y)
{
return new Vector3(x, y, 0.0f);
}
}
Answer by logicandchaos · Jun 28, 2021 at 02:25 PM
Not sure why you have to use floats, you never explain that.. but you can do a simple cast to convert a float to an int.
float number1=1.45f int number2=(int)number1; //you use (variableType) to perform a simple cast.
so you are a beginner floats have decimals, ints do not. so value of number 2 will be 1
print(number2); //prints 1
wouldnt a cast make me loose data number1=1.45f i dont think loosing coordinates would be good
i use the floats for the offsetting and spacing of my hexmap
what i am really not getting why it needs a int in first place maybe i can change that but i dunno
Your answer
![](https://koobas.hobune.stream/wayback/20220613035543im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NullRefenceException Using Arrays 2 Answers
Pick between two floats 2 Answers
Setting float variable to an Array value 1 Answer
Convert Array into One String (Js) 1 Answer