Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 RichardWyeth · Apr 08, 2019 at 05:59 PM · scripting problemmovement2d gametilemaphexagon

How can I achieve movement on hextile?

Hello guys,

I'm new to Unity 2D, and I'm completely stuck with my experiment creating movement on my hexagonal tilemap. So far I managed to create a movement for the player, which works on mouse clicks. For short, I click to a tile, and the player goes there, exactly to the middle. This is what I wanted to achieve, and so far so good.

Next I wanted to create something of a path finding system for the player, and I tried to look for an example using this new system, but I couldn't find any. Right now, I have something of a half solution. My player goes to the tile, finds his way, but only if clicked as much on the tile, as much steps the player needs to do.

In short, the movement's not going trough, only step by step.

I realized, that probably my starting position not updateing correctly, (right now it updates on mouseclick, provideing the solution above), but if I update it anywhere else ( f.e. at the begining of void Update,) the player will go to the tile automatically, but not to the exact middle, only to the edges.

I only tested the pathfinding, if we are going to a destination of the same x but different y value.

Can you help me please?

And if you can give me a tutorial for pathfinding like this, I would be grateful. I now there is an A* algorithm or what, but I didn't really understand it first, and now about catlikecoding, and quill youtube series, but they use their own presets, and the new tilemap system.

Thank you.

Here is my code:

 public GameObject tileObject;
     private Tilemap hexagon;
 
     //Player mozgatás destination vektora
     public Vector3Int moveFinal;
     //Lépés per kör
     public Vector3 move;
  
     Vector3Int starting_Tile;
 
 void Start()
     {
         //Tilamap
         hexagon = tileObject.GetComponent<Tilemap>();
     }
 
 
 // Update is called once per frame
     void Update()
     {
         // Pathfinding();
 
         if (Input.GetMouseButtonUp(0))
         {
             //Where I clicked
             Vector3 clicked = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             clicked.z = 0;
 
 
 
             //Where I want to go
             Vector3Int destination_Tile = hexagon.WorldToCell(clicked);
             moveFinal = destination_Tile;
 
            // The Y distance of the two tiles
             int distance_y = destination_Tile.y - starting_Tile.y;
            // The X distance of the two tiles
             int distance_x = destination_Tile.x - starting_Tile.x;
 
             Debug.Log("Kezdő Tile: " + starting_Tile + "Destination Tile: " + destination_Tile + "Y távolság: " + distance_y + "X távolság: " + distance_x);
 
             //Starting position of the player
             starting_Tile = hexagon.WorldToCell(transform.position);
 
 
         }
 
         //Neighbour Tiles of player position, if Y is even number
         Dictionary<int, Vector3Int> szomszedok = new Dictionary<int, Vector3Int>();
         szomszedok.Add(1, new Vector3Int(starting_Tile.x - 1, starting_Tile.y, starting_Tile.z));
         szomszedok.Add(2, new Vector3Int(starting_Tile.x - 1, starting_Tile.y + 1, starting_Tile.z));
         szomszedok.Add(3, new Vector3Int(starting_Tile.x, starting_Tile.y + 1, starting_Tile.z));
         szomszedok.Add(4, new Vector3Int(starting_Tile.x + 1, starting_Tile.y, starting_Tile.z));
         szomszedok.Add(5, new Vector3Int(starting_Tile.x, starting_Tile.y - 1, starting_Tile.z));
         szomszedok.Add(6, new Vector3Int(starting_Tile.x - 1, starting_Tile.y - 1, starting_Tile.z));
 
         //Neighbour Tiles of player position if Y is odd number
         Dictionary<int, Vector3Int> szomszedok_paratlan = new Dictionary<int, Vector3Int>();
         szomszedok_paratlan.Add(1, new Vector3Int(starting_Tile.x - 1, starting_Tile.y, starting_Tile.z));
         szomszedok_paratlan.Add(2, new Vector3Int(starting_Tile.x, starting_Tile.y + 1, starting_Tile.z));
         szomszedok_paratlan.Add(3, new Vector3Int(starting_Tile.x + 1, starting_Tile.y + 1, starting_Tile.z));
         szomszedok_paratlan.Add(4, new Vector3Int(starting_Tile.x + 1, starting_Tile.y, starting_Tile.z));
         szomszedok_paratlan.Add(5, new Vector3Int(starting_Tile.x + 1, starting_Tile.y - 1, starting_Tile.z));
         szomszedok_paratlan.Add(6, new Vector3Int(starting_Tile.x, starting_Tile.y - 1, starting_Tile.z));
 
 
 
         //Movement ???
         for (int i = 0; i < 3; i++)
         {
          
 
             if (moveFinal.x == starting_Tile.x)
             {
                 if (moveFinal.y > starting_Tile.y)
                 {
                     Vector3 moveing = hexagon.CellToWorld(szomszedok[3]);
                     transform.position = Vector3.MoveTowards(transform.position, moveing, Time.deltaTime * (float)0.5);
                 }
             }
         }
 
     }
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

0 Replies

· Add your reply
  • Sort: 

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

235 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 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

how to shoot bullets from circumference of a circle ? 1 Answer

How do I access the exact tile my player is colliding with?,How can i get access to the specific tile my player is colliding with? 0 Answers

Find Tilemap Player is Currently At 0 Answers

Tilemap Collider 2D 2 Answers

(Localization System) Change Language Problem 0 Answers


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