├── .gitattributes ├── README.md ├── _scripts ├── PlayerInput.cs ├── HexCoordinates.cs ├── Hex.cs ├── SelectionManager.cs ├── MovementSystem.cs ├── HexGrid.cs ├── Unit.cs ├── GraphSearch.cs ├── GlowHighlight.cs └── UnitManager.cs └── LICENSE /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hex Grid Movement in Unity 2 | [![Tutorial Sectiont 1](http://img.youtube.com/vi/htZijEO7ZmE/hqdefault.jpg)](https://youtu.be/htZijEO7ZmE) 3 | 4 |

How to createa s imple hex grid movmeent system in Unity 2021 5 | 6 |

Attribution: 7 | Made by Sunny Valley Studio 8 | -------------------------------------------------------------------------------- /_scripts/PlayerInput.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.Events; 5 | 6 | public class PlayerInput : MonoBehaviour 7 | { 8 | public UnityEvent PointerClick; 9 | 10 | void Update() 11 | { 12 | DetectMouseClick(); 13 | } 14 | 15 | private void DetectMouseClick() 16 | { 17 | if (Input.GetMouseButtonDown(0)) 18 | { 19 | Vector3 mousePos = Input.mousePosition; 20 | PointerClick?.Invoke(mousePos); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /_scripts/HexCoordinates.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | 6 | public class HexCoordinates : MonoBehaviour 7 | { 8 | public static float xOffset = 2, yOffset = 1, zOffset = 1.73f; 9 | 10 | internal Vector3Int GetHexCoords() 11 | => offsetCoordinates; 12 | 13 | [Header("Offset coordinates")] 14 | [SerializeField] 15 | private Vector3Int offsetCoordinates; 16 | 17 | private void Awake() 18 | { 19 | offsetCoordinates = ConvertPositionToOffset(transform.position); 20 | } 21 | 22 | public static Vector3Int ConvertPositionToOffset(Vector3 position) 23 | { 24 | int x = Mathf.CeilToInt(position.x / xOffset); 25 | int y = Mathf.RoundToInt(position.y / yOffset); 26 | int z = Mathf.RoundToInt(position.z / zOffset); 27 | return new Vector3Int(x, y, z); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 SunnyValleyStudio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /_scripts/Hex.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | 6 | [SelectionBase] 7 | public class Hex : MonoBehaviour 8 | { 9 | [SerializeField] 10 | private GlowHighlight highlight; 11 | private HexCoordinates hexCoordinates; 12 | 13 | [SerializeField] 14 | private HexType hexType; 15 | 16 | public Vector3Int HexCoords => hexCoordinates.GetHexCoords(); 17 | 18 | public int GetCost() 19 | => hexType switch 20 | { 21 | HexType.Difficult => 20, 22 | HexType.Default => 10, 23 | HexType.Road => 5, 24 | _ => throw new Exception($"Hex of type {hexType} not supported") 25 | }; 26 | 27 | public bool IsObstacle() 28 | { 29 | return this.hexType == HexType.Obstacle; 30 | } 31 | 32 | private void Awake() 33 | { 34 | hexCoordinates = GetComponent(); 35 | highlight = GetComponent(); 36 | } 37 | public void EnableHighlight() 38 | { 39 | highlight.ToggleGlow(true); 40 | } 41 | 42 | public void DisableHighlight() 43 | { 44 | highlight.ToggleGlow(false); 45 | } 46 | 47 | internal void ResetHighlight() 48 | { 49 | highlight.ResetGlowHighlight(); 50 | } 51 | 52 | internal void HighlightPath() 53 | { 54 | highlight.HighlightValidPath(); 55 | } 56 | } 57 | 58 | public enum HexType 59 | { 60 | None, 61 | Default, 62 | Difficult, 63 | Road, 64 | Water, 65 | Obstacle 66 | } -------------------------------------------------------------------------------- /_scripts/SelectionManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | using UnityEngine.Events; 6 | 7 | public class SelectionManager : MonoBehaviour 8 | { 9 | [SerializeField] 10 | private Camera mainCamera; 11 | 12 | public LayerMask selectionMask; 13 | 14 | 15 | public UnityEvent OnUnitSelected; 16 | public UnityEvent TerrainSelected; 17 | 18 | private void Awake() 19 | { 20 | if (mainCamera == null) 21 | mainCamera = Camera.main; 22 | } 23 | 24 | public void HandleClick(Vector3 mousePosition) 25 | { 26 | GameObject result; 27 | if (FindTarget(mousePosition, out result)) 28 | { 29 | if (UnitSelected(result)) 30 | { 31 | OnUnitSelected?.Invoke(result); 32 | } 33 | else 34 | { 35 | TerrainSelected?.Invoke(result); 36 | } 37 | } 38 | } 39 | 40 | private bool UnitSelected(GameObject result) 41 | { 42 | return result.GetComponent() != null; 43 | } 44 | 45 | private bool FindTarget(Vector3 mousePosition, out GameObject result) 46 | { 47 | RaycastHit hit; 48 | Ray ray = mainCamera.ScreenPointToRay(mousePosition); 49 | if (Physics.Raycast(ray, out hit, 100, selectionMask)) 50 | { 51 | result = hit.collider.gameObject; 52 | return true; 53 | } 54 | result = null; 55 | return false; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /_scripts/MovementSystem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using UnityEngine; 5 | 6 | public class MovementSystem : MonoBehaviour 7 | { 8 | private BFSResult movementRange = new BFSResult(); 9 | private List currentPath = new List(); 10 | 11 | public void HideRange(HexGrid hexGrid) 12 | { 13 | foreach (Vector3Int hexPosition in movementRange.GetRangePositions()) 14 | { 15 | hexGrid.GetTileAt(hexPosition).DisableHighlight(); 16 | } 17 | movementRange = new BFSResult(); 18 | } 19 | 20 | public void ShowRange(Unit selectedUnit, HexGrid hexGrid) 21 | { 22 | CalcualteRange(selectedUnit, hexGrid); 23 | 24 | Vector3Int unitPos = hexGrid.GetClosestHex(selectedUnit.transform.position); 25 | 26 | foreach (Vector3Int hexPosition in movementRange.GetRangePositions()) 27 | { 28 | if (unitPos == hexPosition) 29 | continue; 30 | hexGrid.GetTileAt(hexPosition).EnableHighlight(); 31 | } 32 | } 33 | 34 | public void CalcualteRange(Unit selectedUnit, HexGrid hexGrid) 35 | { 36 | movementRange = GraphSearch.BFSGetRange(hexGrid, hexGrid.GetClosestHex(selectedUnit.transform.position), selectedUnit.MovementPoints); 37 | } 38 | 39 | 40 | public void ShowPath(Vector3Int selectedHexPosition, HexGrid hexGrid) 41 | { 42 | if (movementRange.GetRangePositions().Contains(selectedHexPosition)) 43 | { 44 | foreach (Vector3Int hexPosition in currentPath) 45 | { 46 | hexGrid.GetTileAt(hexPosition).ResetHighlight(); 47 | } 48 | currentPath = movementRange.GetPathTo(selectedHexPosition); 49 | foreach (Vector3Int hexPosition in currentPath) 50 | { 51 | hexGrid.GetTileAt(hexPosition).HighlightPath(); 52 | } 53 | } 54 | } 55 | 56 | public void MoveUnit(Unit selectedUnit, HexGrid hexGrid) 57 | { 58 | Debug.Log("Moving unit " + selectedUnit.name); 59 | selectedUnit.MoveThroughPath(currentPath.Select(pos => hexGrid.GetTileAt(pos).transform.position).ToList()); 60 | 61 | } 62 | 63 | public bool IsHexInRange(Vector3Int hexPosition) 64 | { 65 | return movementRange.IsHexPositionInRange(hexPosition); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /_scripts/HexGrid.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | 6 | public class HexGrid : MonoBehaviour 7 | { 8 | Dictionary hexTileDict = new Dictionary(); 9 | Dictionary> hexTileNeighboursDict = new Dictionary>(); 10 | 11 | private void Start() 12 | { 13 | foreach (Hex hex in FindObjectsOfType()) 14 | { 15 | hexTileDict[hex.HexCoords] = hex; 16 | } 17 | 18 | 19 | } 20 | 21 | public Hex GetTileAt(Vector3Int hexCoordinates) 22 | { 23 | Hex result = null; 24 | hexTileDict.TryGetValue(hexCoordinates, out result); 25 | return result; 26 | } 27 | 28 | public List GetNeighboursFor(Vector3Int hexCoordinates) 29 | { 30 | if (hexTileDict.ContainsKey(hexCoordinates) == false) 31 | return new List(); 32 | 33 | if (hexTileNeighboursDict.ContainsKey(hexCoordinates)) 34 | return hexTileNeighboursDict[hexCoordinates]; 35 | 36 | hexTileNeighboursDict.Add(hexCoordinates, new List()); 37 | 38 | foreach (Vector3Int direction in Direction.GetDirectionList(hexCoordinates.z)) 39 | { 40 | if (hexTileDict.ContainsKey(hexCoordinates + direction)) 41 | { 42 | hexTileNeighboursDict[hexCoordinates].Add(hexCoordinates + direction); 43 | } 44 | } 45 | return hexTileNeighboursDict[hexCoordinates]; 46 | } 47 | 48 | public Vector3Int GetClosestHex(Vector3 worldposition) 49 | { 50 | worldposition.y = 0; 51 | return HexCoordinates.ConvertPositionToOffset(worldposition); 52 | } 53 | } 54 | 55 | public static class Direction 56 | { 57 | public static List directionsOffsetOdd = new List 58 | { 59 | new Vector3Int(-1,0,1), //N1 60 | new Vector3Int(0,0,1), //N2 61 | new Vector3Int(1,0,0), //E 62 | new Vector3Int(0,0,-1), //S2 63 | new Vector3Int(-1,0,-1), //S1 64 | new Vector3Int(-1,0,0), //W 65 | }; 66 | 67 | public static List directionsOffsetEven = new List 68 | { 69 | new Vector3Int(0,0,1), //N1 70 | new Vector3Int(1,0,1), //N2 71 | new Vector3Int(1,0,0), //E 72 | new Vector3Int(1,0,-1), //S2 73 | new Vector3Int(0,0,-1), //S1 74 | new Vector3Int(-1,0,0), //W 75 | }; 76 | 77 | public static List GetDirectionList(int z) 78 | => z % 2 == 0 ? directionsOffsetEven : directionsOffsetOdd; 79 | } 80 | -------------------------------------------------------------------------------- /_scripts/Unit.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | 6 | [SelectionBase] 7 | public class Unit : MonoBehaviour 8 | { 9 | [SerializeField] 10 | private int movementPoints = 20; 11 | public int MovementPoints { get => movementPoints; } 12 | 13 | [SerializeField] 14 | private float movementDuration = 1, rotationDuration = 0.3f; 15 | 16 | private GlowHighlight glowHighlight; 17 | private Queue pathPositions = new Queue(); 18 | 19 | public event Action MovementFinished; 20 | 21 | private void Awake() 22 | { 23 | glowHighlight = GetComponent(); 24 | } 25 | 26 | public void Deselect() 27 | { 28 | glowHighlight.ToggleGlow(false); 29 | } 30 | 31 | public void Select() 32 | { 33 | glowHighlight.ToggleGlow(); 34 | } 35 | 36 | public void MoveThroughPath(List currentPath) 37 | { 38 | pathPositions = new Queue(currentPath); 39 | Vector3 firstTarget = pathPositions.Dequeue(); 40 | StartCoroutine(RotationCoroutine(firstTarget, rotationDuration)); 41 | } 42 | 43 | private IEnumerator RotationCoroutine(Vector3 endPosition, float rotationDuration) 44 | { 45 | Quaternion startRotation = transform.rotation; 46 | endPosition.y = transform.position.y; 47 | Vector3 direction = endPosition - transform.position; 48 | Quaternion endRotation = Quaternion.LookRotation(direction, Vector3.up); 49 | 50 | if (Mathf.Approximately(Mathf.Abs(Quaternion.Dot(startRotation, endRotation)), 1.0f) == false) 51 | { 52 | float timeElapsed = 0; 53 | while (timeElapsed < rotationDuration) 54 | { 55 | timeElapsed += Time.deltaTime; 56 | float lerpStep = timeElapsed / rotationDuration; // 0-1 57 | transform.rotation = Quaternion.Lerp(startRotation, endRotation, lerpStep); 58 | yield return null; 59 | } 60 | transform.rotation = endRotation; 61 | } 62 | StartCoroutine(MovementCoroutine(endPosition)); 63 | } 64 | 65 | private IEnumerator MovementCoroutine(Vector3 endPosition) 66 | { 67 | Vector3 startPosition = transform.position; 68 | endPosition.y = startPosition.y; 69 | float timeElapsed = 0; 70 | 71 | while (timeElapsed < movementDuration) 72 | { 73 | timeElapsed += Time.deltaTime; 74 | float lerpStep = timeElapsed / movementDuration; 75 | transform.position = Vector3.Lerp(startPosition, endPosition, lerpStep); 76 | yield return null; 77 | } 78 | transform.position = endPosition; 79 | 80 | if (pathPositions.Count > 0) 81 | { 82 | Debug.Log("Selecting the next position!"); 83 | StartCoroutine(RotationCoroutine(pathPositions.Dequeue(), rotationDuration)); 84 | } 85 | else 86 | { 87 | Debug.Log("Movement finished!"); 88 | MovementFinished?.Invoke(this); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /_scripts/GraphSearch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using UnityEngine; 6 | 7 | public class GraphSearch 8 | { 9 | public static BFSResult BFSGetRange(HexGrid hexGrid, Vector3Int startPoint, int movementPoints) 10 | { 11 | Dictionary visitedNodes = new Dictionary(); 12 | Dictionary costSoFar = new Dictionary(); 13 | Queue nodesToVisitQueue = new Queue(); 14 | 15 | nodesToVisitQueue.Enqueue(startPoint); 16 | costSoFar.Add(startPoint, 0); 17 | visitedNodes.Add(startPoint, null); 18 | 19 | while (nodesToVisitQueue.Count > 0) 20 | { 21 | Vector3Int currentNode = nodesToVisitQueue.Dequeue(); 22 | foreach (Vector3Int neighbourPosition in hexGrid.GetNeighboursFor(currentNode)) 23 | { 24 | if (hexGrid.GetTileAt(neighbourPosition).IsObstacle()) 25 | continue; 26 | 27 | int nodeCost = hexGrid.GetTileAt(neighbourPosition).GetCost(); 28 | int currentCost = costSoFar[currentNode]; 29 | int newCost = currentCost + nodeCost; 30 | 31 | if (newCost <= movementPoints) 32 | { 33 | if (!visitedNodes.ContainsKey(neighbourPosition)) 34 | { 35 | visitedNodes[neighbourPosition] = currentNode; 36 | costSoFar[neighbourPosition] = newCost; 37 | nodesToVisitQueue.Enqueue(neighbourPosition); 38 | } 39 | else if (costSoFar[neighbourPosition] > newCost) 40 | { 41 | costSoFar[neighbourPosition] = newCost; 42 | visitedNodes[neighbourPosition] = currentNode; 43 | } 44 | } 45 | } 46 | } 47 | return new BFSResult { visitedNodesDict = visitedNodes }; 48 | } 49 | 50 | 51 | public static List GeneratePathBFS(Vector3Int current, Dictionary visitedNodesDict) 52 | { 53 | List path = new List(); 54 | path.Add(current); 55 | while (visitedNodesDict[current] != null) 56 | { 57 | path.Add(visitedNodesDict[current].Value); 58 | current = visitedNodesDict[current].Value; 59 | } 60 | path.Reverse(); 61 | return path.Skip(1).ToList(); 62 | } 63 | } 64 | 65 | public struct BFSResult 66 | { 67 | public Dictionary visitedNodesDict; 68 | 69 | public List GetPathTo(Vector3Int destination) 70 | { 71 | if (visitedNodesDict.ContainsKey(destination) == false) 72 | return new List(); 73 | return GraphSearch.GeneratePathBFS(destination, visitedNodesDict); 74 | } 75 | 76 | public bool IsHexPositionInRange(Vector3Int position) 77 | { 78 | return visitedNodesDict.ContainsKey(position); 79 | } 80 | 81 | public IEnumerable GetRangePositions() 82 | => visitedNodesDict.Keys; 83 | } -------------------------------------------------------------------------------- /_scripts/GlowHighlight.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | 6 | public class GlowHighlight : MonoBehaviour 7 | { 8 | Dictionary glowMaterialDictionary = new Dictionary(); 9 | Dictionary originalMaterialDictionary = new Dictionary(); 10 | 11 | Dictionary cachedGlowMaterials = new Dictionary(); 12 | 13 | public Material glowMaterial; 14 | 15 | private bool isGlowing = false; 16 | 17 | private Color validSpaceColor = Color.green; 18 | private Color originalGlowColor; 19 | 20 | private void Awake() 21 | { 22 | PrepareMaterialDictionaries(); 23 | originalGlowColor = glowMaterial.GetColor("_GlowColor"); 24 | } 25 | 26 | private void PrepareMaterialDictionaries() 27 | { 28 | foreach (Renderer renderer in GetComponentsInChildren()) 29 | { 30 | Material[] originalMaterials = renderer.materials; 31 | originalMaterialDictionary.Add(renderer, originalMaterials); 32 | 33 | Material[] newMaterials = new Material[renderer.materials.Length]; 34 | 35 | for (int i = 0; i < originalMaterials.Length; i++) 36 | { 37 | Material mat = null; 38 | if (cachedGlowMaterials.TryGetValue(originalMaterials[i].color, out mat) == false) 39 | { 40 | mat = new Material(glowMaterial); 41 | //By default, Unity considers a color with the property name name "_Color" to be the main color 42 | mat.color = originalMaterials[i].color; 43 | cachedGlowMaterials[mat.color] = mat; 44 | } 45 | newMaterials[i] = mat; 46 | } 47 | glowMaterialDictionary.Add(renderer, newMaterials); 48 | } 49 | } 50 | 51 | internal void HighlightValidPath() 52 | { 53 | if (isGlowing == false) 54 | return; 55 | foreach (Renderer renderer in glowMaterialDictionary.Keys) 56 | { 57 | foreach (Material item in glowMaterialDictionary[renderer]) 58 | { 59 | item.SetColor("_GlowColor", validSpaceColor); 60 | } 61 | } 62 | } 63 | 64 | internal void ResetGlowHighlight() 65 | { 66 | foreach (Renderer renderer in glowMaterialDictionary.Keys) 67 | { 68 | foreach (Material item in glowMaterialDictionary[renderer]) 69 | { 70 | item.SetColor("_GlowColor", originalGlowColor); 71 | } 72 | } 73 | } 74 | 75 | public void ToggleGlow() 76 | { 77 | if (isGlowing == false) 78 | { 79 | ResetGlowHighlight(); 80 | foreach (Renderer renderer in originalMaterialDictionary.Keys) 81 | { 82 | renderer.materials = glowMaterialDictionary[renderer]; 83 | } 84 | 85 | } 86 | else 87 | { 88 | foreach (Renderer renderer in originalMaterialDictionary.Keys) 89 | { 90 | renderer.materials = originalMaterialDictionary[renderer]; 91 | } 92 | } 93 | isGlowing = !isGlowing; 94 | } 95 | 96 | public void ToggleGlow(bool state) 97 | { 98 | if (isGlowing == state) 99 | return; 100 | isGlowing = !state; 101 | ToggleGlow(); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /_scripts/UnitManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class UnitManager : MonoBehaviour 6 | { 7 | [SerializeField] 8 | private HexGrid hexGrid; 9 | 10 | [SerializeField] 11 | private MovementSystem movementSystem; 12 | 13 | public bool PlayersTurn { get; private set; } = true; 14 | 15 | [SerializeField] 16 | private Unit selectedUnit; 17 | private Hex previouslySelectedHex; 18 | 19 | public void HandleUnitSelected(GameObject unit) 20 | { 21 | if (PlayersTurn == false) 22 | return; 23 | 24 | Unit unitReference = unit.GetComponent(); 25 | 26 | if (CheckIfTheSameUnitSelected(unitReference)) 27 | return; 28 | 29 | PrepareUnitForMovement(unitReference); 30 | } 31 | 32 | private bool CheckIfTheSameUnitSelected(Unit unitReference) 33 | { 34 | if (this.selectedUnit == unitReference) 35 | { 36 | ClearOldSelection(); 37 | return true; 38 | } 39 | return false; 40 | } 41 | 42 | public void HandleTerrainSelected(GameObject hexGO) 43 | { 44 | if (selectedUnit == null || PlayersTurn == false) 45 | { 46 | return; 47 | } 48 | 49 | Hex selectedHex = hexGO.GetComponent(); 50 | 51 | if (HandleHexOutOfRange(selectedHex.HexCoords) || HandleSelectedHexIsUnitHex(selectedHex.HexCoords)) 52 | return; 53 | 54 | HandleTargetHexSelected(selectedHex); 55 | 56 | } 57 | 58 | private void PrepareUnitForMovement(Unit unitReference) 59 | { 60 | if (this.selectedUnit != null) 61 | { 62 | ClearOldSelection(); 63 | } 64 | 65 | this.selectedUnit = unitReference; 66 | this.selectedUnit.Select(); 67 | movementSystem.ShowRange(this.selectedUnit, this.hexGrid); 68 | } 69 | 70 | private void ClearOldSelection() 71 | { 72 | previouslySelectedHex = null; 73 | this.selectedUnit.Deselect(); 74 | movementSystem.HideRange(this.hexGrid); 75 | this.selectedUnit = null; 76 | 77 | } 78 | 79 | private void HandleTargetHexSelected(Hex selectedHex) 80 | { 81 | if (previouslySelectedHex == null || previouslySelectedHex != selectedHex) 82 | { 83 | previouslySelectedHex = selectedHex; 84 | movementSystem.ShowPath(selectedHex.HexCoords, this.hexGrid); 85 | } 86 | else 87 | { 88 | movementSystem.MoveUnit(selectedUnit, this.hexGrid); 89 | PlayersTurn = false; 90 | selectedUnit.MovementFinished += ResetTurn; 91 | ClearOldSelection(); 92 | 93 | } 94 | } 95 | 96 | private bool HandleSelectedHexIsUnitHex(Vector3Int hexPosition) 97 | { 98 | if (hexPosition == hexGrid.GetClosestHex(selectedUnit.transform.position)) 99 | { 100 | selectedUnit.Deselect(); 101 | ClearOldSelection(); 102 | return true; 103 | } 104 | return false; 105 | } 106 | 107 | private bool HandleHexOutOfRange(Vector3Int hexPosition) 108 | { 109 | if (movementSystem.IsHexInRange(hexPosition) == false) 110 | { 111 | Debug.Log("Hex Out of range!"); 112 | return true; 113 | } 114 | return false; 115 | } 116 | 117 | private void ResetTurn(Unit selectedUnit) 118 | { 119 | selectedUnit.MovementFinished -= ResetTurn; 120 | PlayersTurn = true; 121 | } 122 | } 123 | --------------------------------------------------------------------------------