Search Unity

Hexagonal movement

Discussion in 'Navigation' started by Yamada19, Apr 3, 2019.

  1. Yamada19

    Yamada19

    Joined:
    Mar 15, 2019
    Posts:
    3
    Hello,
    I've never been using unity before and now I decided to try making a simple 2D game. I'm trying to make hexagonal movement by one tile using mouse click but there is no progress, so I hope to get some help here. I've watched some YouTube tutorials, but I think I need something concrete to make an understanding.

    I was wondering if it is more simpler to make it by creating tiles as 3D objects or is there any way to make it using hexagonal grid?

    I hope you understood my problem.
    qdqdqd.PNG
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Ok, first off you'll need to register which tile you're clicking.
    The Grid system has ways to easily give you cell centers based on a position: GetCellCenterWorld.
    If you use this and make sure that the target cell is only 1 cell away from the current cell, you can do 1 by 1 movement.
    Pseudo-code you place on your pawn:
    Code (CSharp):
    1. if(Input.GetMouseButtonDown(0)) //left mouse click
    2. {
    3.     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.     if(Physics.Raycast(ray, out RaycastHit hit) //Cast a ray from mouse pos down
    5.     {
    6.         Vector3 newCenter = grid.GetCellCenterWorld(hit.position);
    7.         if(transform.position - newCenter <= grid.cellSize)
    8.         {
    9.             Move(newCenter);
    10.         }
    11.     }
    12. }
    13. Move(Vector3 position)
    14. {
    15.     //Move to the new position, handle this however you like.
    16. }
    17.  
     
    Yamada19 likes this.
  3. Yamada19

    Yamada19

    Joined:
    Mar 15, 2019
    Posts:
    3
    Thank you!