private void Update() { _movementInput = Vector3.zero; _movementInput.z = 0; float horizontal = Input.GetAxisRaw("Horizontal"); float vertical = Input.GetAxisRaw("Vertical"); _movementInput.x = horizontal; _movementInput.y = vertical; if (_movementInput != Vector3.zero) { _sprintController.Update("Sprint", _movementInput); float speed = _sprintController.IsCurrentlyUsing ? _sprintSpeed : _walkSpeed; Vector3 newMovement = _movementInput.normalized * speed * Time.deltaTime; Vector3 oldPos = transform.position; transform.position += newMovement; if (!GridChecker.IsPositionAllowed(transform.position)) { transform.position = oldPos; } else { _debugText.text = "Grid: " + (GridChecker.GetGridIndexFromPosition(transform.position) + 1).ToString() + GridChecker.GetGridCellFromPosition(transform.position).ToString(); } } else { _sprintController.Update("Sprint", Vector3.zero); } }
// Use this for initialization void Start() { _player = GameObject.FindObjectOfType <PlayerController>(); GridChecker = GameObject.FindObjectOfType <GridChecker>(); GridChecker.AddEnemy(this); EnemyColor = Color.white; }
public List <Node> FindPath(Vector2 startPos, Vector2 targetPos) { Node startNode = GridChecker.GetNodeFromWorldPosition(startPos); Node targetNode = GridChecker.GetNodeFromWorldPosition(targetPos); if (startNode == null || targetNode == null) { return(null); } List <Node> openSet = new List <Node>(); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet[0]; for (int i = 1; i < openSet.Count; i++) { if (openSet[i].FCost < currentNode.FCost || openSet[i].FCost == currentNode.FCost && openSet[i].hCost < currentNode.hCost) { currentNode = openSet[i]; } } openSet.Remove(currentNode); closedSet.Add(currentNode); if (currentNode == targetNode) { return(RetracePath(startNode, targetNode)); } foreach (Node neighbourNode in grid.GetNeighbourNodes(currentNode)) { if (!neighbourNode.walkable || closedSet.Contains(neighbourNode)) { continue; } int newCostToNeighbour = currentNode.gCost + GetTargetDistance(currentNode, neighbourNode); if (newCostToNeighbour < neighbourNode.gCost || !openSet.Contains(neighbourNode)) { neighbourNode.gCost = newCostToNeighbour; neighbourNode.hCost = GetTargetDistance(neighbourNode, targetNode); neighbourNode.parent = currentNode; if (!openSet.Contains(neighbourNode)) { openSet.Add(neighbourNode); } } } } return(null); }
void MakeCaptureColldiers() { Util.ForI((i) => { GameObject _go = new GameObject(); _go.transform.SetParent(transform); _go.transform.position = transform.position; BoxCollider _childColl = _go.AddComponent <BoxCollider>(); GridChecker _checker = _go.AddComponent <GridChecker>(); _childColl.isTrigger = true; _childColl.size = new Vector3(transform.lossyScale.x, _segmentHeight, transform.lossyScale.z); _go.transform.position = new Vector3(transform.position.x, transform.position.y + (_segmentHeight + transform.lossyScale.y) * 0.5f + i * _segmentHeight, transform.position.z); _go.layer = 16; _checkers.Add(_checker); }, Mathf.FloorToInt(_totalHeight / _segmentHeight)); }
private void FindPath() { _currentNodeIndex = 0; _pathFinder = new Pathfinding(); _pathFinder.grid = FindObjectOfType <DungeonRoomGrid>(); _currentPath = _pathFinder.FindPath(_grid.transform.position, _grid.transform.position); Debug.Log("CellTarget: " + GridChecker.GetGridCellFromPosition(_currentPath[0].worldPosition)); if (_currentPath == null) { _foundPath = false; } else { _foundPath = true; } //Debug.Log("Found Path: " + _foundPath); }