Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by patspfp · Jun 28, 2021 at 02:21 PM · arrayfloatconversion

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image patspfp · Jun 28, 2021 at 10:50 PM 0
Share

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 alt text

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Cannot implicitly convert type `float' to `int 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges