public void Update() { if (path.Count > 0) { if (grid.NodeFromWorldPoint(pathCheck.position) == grid.NodeFromWorldPoint(target.position)) { path.Clear(); } if (grid.NodeFromWorldPoint(pathCheck.position) == path[currentIndex] || Vector2.Distance(pathCheck.position, path[currentIndex].worldPosition) <= grid.gridSpacing) { currentIndex++; } for (int i = 0; i < path[currentIndex - 1].links.Count; i++) { if (path[currentIndex - 1].links[i].endNode == path[currentIndex].gridIndex) { currentLink = path[currentIndex].links[i]; currentLinkIndex = currentLink.endNode; } } if (_controller.isGrounded) { _velocity.y = 0; } if (path[currentIndex].worldPosition.x < transform.position.x) { normalizedHorizontalSpeed = -1; } else if (path[currentIndex].worldPosition.x > transform.position.x) { normalizedHorizontalSpeed = 1; } if (currentLink != null && currentLink.linkType == PathLinkType.drop && _controller.isGrounded) { _velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity); } if (path[currentIndex].worldPosition.y - pathCheck.position.y >= grid.gridSpacing && path[currentIndex].worldPosition.x - pathCheck.position.x <= grid.nodeRadius && path[currentIndex].worldPosition.x - pathCheck.position.x >= -grid.nodeRadius && _controller.isGrounded) { _velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity); } // apply horizontal speed smoothing it. dont really do this with Lerp. Use SmoothDamp or something that provides more control float smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction? _velocity.x = Mathf.Lerp(_velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor); // apply gravity before moving _velocity.y += gravity * Time.deltaTime; if (currentLink != null && currentLink.linkType == PathLinkType.drop) { _velocity.y = gravity / 10; _controller.ignoreOneWayPlatformsThisFrame = true; } _controller.move(_velocity * Time.deltaTime); // grab our current _velocity to use as a base for all calculations _velocity = _controller.velocity; } else { normalizedHorizontalSpeed = 0; // apply horizontal speed smoothing it. dont really do this with Lerp. Use SmoothDamp or something that provides more control float smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction? _velocity.x = Mathf.Lerp(_velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor); // apply gravity before moving _velocity.y += gravity * Time.deltaTime; _controller.move(_velocity * Time.deltaTime); // grab our current _velocity to use as a base for all calculations _velocity = _controller.velocity; } }
public void FindPath(Vector2 startPos, Vector2 targetPos) { PlatformerPathNode startNode = grid.NodeFromWorldPoint(startPos); PlatformerPathNode endNode = grid.NodeFromWorldPoint(targetPos); List <PlatformerPathNode> openSet = new List <PlatformerPathNode>(); HashSet <PlatformerPathNode> closedSet = new HashSet <PlatformerPathNode>(); PlatformerPathNode startOffset = null; PlatformerPathNode endOffset = null; if (startNode.nodeType == PlatNodeType.none || startNode.nodeType == PlatNodeType.blocked) { startOffset = grid.FindNeighbouringValidNode(startNode); if (startOffset == null) { if (startNode.nodeType == PlatNodeType.none) { startOffset = grid.FindValidNodeBelow(startNode); } else { startOffset = grid.FindNeighbouringOpenNode(startNode); if (startOffset == null && startNode.nodeType == PlatNodeType.blocked) { Debug.Log("Something has gone horribly wrong with the starting node, aborting."); } else { startOffset = grid.FindValidNodeBelow(startOffset); } } } } if (endNode.nodeType == PlatNodeType.none || endNode.nodeType == PlatNodeType.blocked) { endOffset = grid.FindNeighbouringValidNode(endNode); if (endOffset == null) { if (endNode.nodeType == PlatNodeType.none) { endOffset = grid.FindValidNodeBelow(endNode); } else { endOffset = grid.FindNeighbouringOpenNode(endNode); if (endOffset == null && endNode.nodeType == PlatNodeType.blocked) { Debug.Log("Something has gone horribly wrong with the target node, aborting."); } else { endOffset = grid.FindValidNodeBelow(endOffset); } } } } if (startOffset != null) { startNode = startOffset; } if (endOffset != null) { endNode = endOffset; } openSet.Add(startNode); while (openSet.Count > 0) { PlatformerPathNode 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 == endNode) { RetracePath(startNode, endNode); return; } for (int i = 0; i < gridData[currentNode.gridIndex].links.Count; i++) { PlatformerPathLink l = gridData[currentNode.gridIndex].links[i]; if (closedSet.Contains(gridData[l.endNode])) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, gridData[l.endNode]); if (newMovementCostToNeighbour < gridData[l.endNode].gCost || !openSet.Contains(gridData[l.endNode])) { gridData[l.endNode].gCost = newMovementCostToNeighbour; gridData[l.endNode].hCost = GetDistance(gridData[l.endNode], endNode); gridData[l.endNode].parent = currentNode.gridIndex; if (!openSet.Contains(gridData[l.endNode])) { openSet.Add(gridData[l.endNode]); } } } } }