/// <summary> /// Unity Update loop. /// </summary> void Update() { RaycastHit?hit = null; bool foundUnit = false; BattleUnit toAttack = null; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] allHits = Physics.RaycastAll(ray, 200f); if (allHits.Length > 0) { for (int i = 0; i < allHits.Length && hit == null; i++) { BattleHex newHit = allHits[i].transform.gameObject.GetComponentInParent <BattleHex>(); if (newHit != null) { hit = allHits[i]; if (newHit.HasUnit && newHit.Unit is AIControlledUnit) { foundUnit = true; if (_attackState == false) { _attackState = true; Cursor.SetCursor(_attackCursor, Vector2.zero, CursorMode.Auto); } BattleHex closestHex = null; float closestDistance = float.MaxValue; foreach (var hex in newHit.Neighbours) { float distance = Vector3.Distance(hex.transform.position, hit.Value.point); if (distance < closestDistance) { closestDistance = distance; closestHex = hex; } } if (closestHex != null) { toAttack = newHit.Unit; if (_currentHit != closestHex) { if (_currentHit == null) { _currentHit = closestHex; _currentHit.MouseEnter(); } else { _currentHit.MouseLeave(); _currentHit = closestHex; _currentHit.MouseEnter(); } } } else if (_currentHit != null) { _currentHit.MouseLeave(); _currentHit = null; } } else if (_currentHit != newHit) { if (_currentHit == null) { _currentHit = newHit; _currentHit.MouseEnter(); } else { _currentHit.MouseLeave(); _currentHit = newHit; _currentHit.MouseEnter(); } } } } } if (hit == null && _currentHit != null) { _currentHit.MouseLeave(); _currentHit = null; } if (!_isLocked && Input.GetMouseButtonDown(MouseButton.Right) && _currentHit != null) { Path <BattleHex> path = null; if (_battleGrid.CurrentUnit.CanTravelTo(_currentHit)) { path = PathFinder.FindPath(_battleGrid.CurrentUnit.CurrentHexTile, _currentHit); } else if (toAttack != null && _battleGrid.CurrentUnit.IsOnTile(_currentHit)) { path = new Path <BattleHex>(_currentHit); } if (path != null) { if (toAttack != null) { _battleGrid.CurrentUnit.TravelPathAndAttack(path.GetPath(), toAttack); } else { _battleGrid.CurrentUnit.TravelPath(path.GetPath()); } Lock(); } } if (foundUnit == false && _attackState) { _attackState = false; Cursor.SetCursor(_regularCursor, Vector2.zero, CursorMode.Auto); } }