I did all the pathfinding system but now my move Action is not working on any character, all the other actions work perfect but i dont have any errors in console and i can´t find the error to be honest.
Let’s start with a look at your Pathfinding.cs and your MoveAction.cs scripts
- Forum User Guides : How to apply code formatting within your post
Okay, this is my pathfinding Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
...
public class Pathfinding : MonoBehaviour
{
public static Pathfinding Instance { get; private set; }
private const int MOVE_STRAIGHT_COST = 10;
private const int MOVE_DIAGONAL_COST = 14;
[SerializeField] private Transform gridDebugObjectPrefab;
[SerializeField] private LayerMask obstaclesLayerMask;
private int width;
private int height;
private float cellSize;
private GridSystem<PathNode> gridSystem;
private void Awake()
{
if (Instance != null)
{
Debug.LogError("Mas de un Path!" + transform + " - " + Instance);
Destroy(gameObject);
return;
}
Instance = this;
}
public void SetUp(int width, int height, float cellSize)
{
this.width = width;
this.height = height;
this.cellSize = cellSize;
gridSystem = new GridSystem<PathNode>(width, height, cellSize,
(GridSystem<PathNode> g, GridPosition gridPosition) => new PathNode(gridPosition));
//gridSystem.CreateDebugObjects(gridDebugObjectPrefab);
for (int x = 0; x < width; x++)
{
for (int z = 0; z < height; z++)
{
GridPosition gridPosition = new GridPosition(x, z);
Vector3 worldPosition = LevelGrid.Instance.GetWorldPosition(gridPosition);
float raycastOffsetDistance = 5f;
if (Physics.Raycast(
worldPosition + Vector3.down * raycastOffsetDistance,
Vector3.up,
raycastOffsetDistance * 2,
obstaclesLayerMask))
{
GetNode(x, z).SetIsWalkable(false);
}
}
}
}
public List<GridPosition> FindPath(GridPosition startGridPosition, GridPosition endGridPosition, out int pathLength)
{
List<PathNode> openList = new List<PathNode>();
List<PathNode> closedList = new List<PathNode>();
PathNode startNode = gridSystem.GetGridObject(startGridPosition);
PathNode endNode = gridSystem.GetGridObject(endGridPosition);
openList.Add(startNode);
for (int x = 0; x < gridSystem.GetWidth(); x++)
{
for (int z = 0; z < gridSystem.GetHeight(); z++)
{
GridPosition gridPosition = new GridPosition(x, z);
PathNode pathNode = gridSystem.GetGridObject(gridPosition);
pathNode.SetGCost(int.MaxValue);
pathNode.SetHCost(0);
pathNode.CalculateFCost();
pathNode.ResetCameFromPathNode();
}
}
startNode.SetGCost(0);
startNode.SetHCost(CalculateDistance(startGridPosition, endGridPosition));
startNode.ResetCameFromPathNode();
while (openList.Count > 0)
{
PathNode currentNode = GetLowestFCostPathNode(openList);
if (currentNode == endNode)
{
//final node
pathLength = endNode.GetFCost();
return CalculatePath(endNode);
}
openList.Remove(currentNode);
closedList.Add(currentNode);
foreach (PathNode neighbourNode in GetNeighbourList(currentNode))
{
if (closedList.Contains(neighbourNode))
{
continue;
}
if (!neighbourNode.IsWalkable())
{
closedList.Add(neighbourNode);
continue;
}
int tentativeGhost =
currentNode.GetGCost() +
CalculateDistance(currentNode.GetGridPosition(), neighbourNode.GetGridPosition());
if (tentativeGhost < neighbourNode.GetGCost())
{
neighbourNode.SetCameFromPathNode(currentNode);
neighbourNode.SetGCost(tentativeGhost);
neighbourNode.SetHCost(CalculateDistance(neighbourNode.GetGridPosition(), endGridPosition));
neighbourNode.CalculateFCost();
if (!openList.Contains(neighbourNode))
{
openList.Add(neighbourNode);
}
}
}
}
//NO PATH FOUND
pathLength = 0;
return null;
}
public int CalculateDistance(GridPosition gridPositionA, GridPosition gridPositionB)
{
GridPosition gridPositionDistance = gridPositionA - gridPositionB;
int xDistance = Mathf.Abs(gridPositionDistance.x);
int zDistance = Mathf.Abs(gridPositionDistance.z);
int remaining = Mathf.Abs(xDistance - zDistance);
return MOVE_DIAGONAL_COST * Mathf.Min(xDistance, zDistance) + MOVE_STRAIGHT_COST * remaining;
}
private PathNode GetLowestFCostPathNode(List<PathNode> pathNodeList)
{
PathNode lowestFCostPathNode = pathNodeList[0];
for (int i = 0; i < pathNodeList.Count; i++)
{
if (pathNodeList[i].GetFCost() < lowestFCostPathNode.GetFCost())
{
lowestFCostPathNode = pathNodeList[i];
}
}
return lowestFCostPathNode;
}
private PathNode GetNode(int x, int z)
{
return gridSystem.GetGridObject(new GridPosition(x, z));
}
private List<PathNode> GetNeighbourList(PathNode currentNode)
{
List<PathNode> neighbourList = new List<PathNode>();
GridPosition gridPosition = currentNode.GetGridPosition();
if (gridPosition.x - 1 >= 0)
{
// LEFT
neighbourList.Add(GetNode(gridPosition.x - 1, gridPosition.z + 0));
if (gridPosition.z - 1 >= 0)
{
// LEFT DOWN
neighbourList.Add(GetNode(gridPosition.x - 1, gridPosition.z - 1));
}
if (gridPosition.z + 1 < gridSystem.GetHeight())
{
// LEFT UP
neighbourList.Add(GetNode(gridPosition.x - 1, gridPosition.z + 1));
}
}
if (gridPosition.x + 1 < gridSystem.GetWidth())
{
// RIGHT
neighbourList.Add(GetNode(gridPosition.x + 1, gridPosition.z + 0));
if (gridPosition.z - 1 >= 0)
{
// RIGHT DOWN
neighbourList.Add(GetNode(gridPosition.x + 1, gridPosition.z - 1));
}
if (gridPosition.z + 1 < gridSystem.GetHeight()) ;
{
// RIGHT UP
neighbourList.Add(GetNode(gridPosition.x + 1, gridPosition.z + 1));
}
}
if (gridPosition.z - 1 >= 0)
{
// DOWN
neighbourList.Add(GetNode(gridPosition.x + 0, gridPosition.z - 1));
}
if (gridPosition.z + 1 < gridSystem.GetHeight())
{
// UP
neighbourList.Add(GetNode(gridPosition.x + 0, gridPosition.z + 1));
}
return neighbourList;
}
private List<GridPosition> CalculatePath(PathNode endNode)
{
List<PathNode> pathNodeList = new List<PathNode>();
pathNodeList.Add(endNode);
PathNode currentNode = endNode;
while (currentNode.GetCameFromPathNode() != null)
{
pathNodeList.Add(currentNode.GetCameFromPathNode());
currentNode = currentNode.GetCameFromPathNode();
}
pathNodeList.Reverse();
List<GridPosition> gridPositionList = new List<GridPosition>();
foreach (PathNode pathNode in pathNodeList)
{
gridPositionList.Add(pathNode.GetGridPosition());
}
return gridPositionList;
}
public bool IsWalkableGridPosition(GridPosition gridPosition)
{
return gridSystem.GetGridObject(gridPosition).IsWalkable();
}
public bool HasPath(GridPosition startGridPosition, GridPosition endGridPosition)
{
return FindPath(startGridPosition, endGridPosition, out int pathLength) != null;
}
public int GetPathLength(GridPosition startGridPosition, GridPosition endGridPosition)
{
FindPath(startGridPosition, endGridPosition, out int pathLength);
return pathLength;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class MoveAction : BaseAction
{
public event EventHandler OnStartMoving;
public event EventHandler OnStopMoving;
[SerializeField] private int maxMoveDistance = 4; // La mayor distancia que se podrá recorrer
private List<Vector3> positionList;
private int currentPositionIndex;
//Posicion del target o unidad
private void Update()
{
if (!isActive)
{
return;
}
Vector3 targetPosition = positionList[currentPositionIndex];
Vector3 moveDirection = (targetPosition - transform.position).normalized;
float rotateSpeed = 10f;
transform.forward = Vector3.Lerp(transform.forward, moveDirection, Time.deltaTime * rotateSpeed);
float stoppingDistance = .1f; //Distancia a la que se irá parando la unidad antes de llegar a su nueva posicion
if (Vector3.Distance(transform.position, targetPosition) > stoppingDistance)
{
float moveSpeed = 4f; // velocidad con la que se mueve la unidad
transform.position += moveDirection * moveSpeed * Time.deltaTime;
}
else
{
currentPositionIndex++;
if (currentPositionIndex >= positionList.Count)
{
OnStopMoving?.Invoke(this, EventArgs.Empty);
ActionComplete();
}
}
}
public override void TakeAction(GridPosition gridPosition,
Action onActionComplete) // Llamamos al metodo MOVER y actualizamos la posicion
{
List<GridPosition> pathGridPositionList =
Pathfinding.Instance.FindPath(unit.GetGridPosition(), gridPosition, out int pathLength);
currentPositionIndex = 0;
positionList = new List<Vector3>();
foreach (GridPosition pathGridPosition in pathGridPositionList)
{
positionList.Add(LevelGrid.Instance.GetWorldPosition(pathGridPosition));
}
OnStartMoving?.Invoke(this, EventArgs.Empty);
ActionStart(onActionComplete);
}
public override List<GridPosition> GetValidActionGridPositionList()
{
List<GridPosition> validGridPositionList = new List<GridPosition>();
GridPosition unitGridPosition = unit.GetGridPosition();
for (int x = -maxMoveDistance; x <= maxMoveDistance; x++)
{
for (int z = -maxMoveDistance; z <= maxMoveDistance; z++)
{
GridPosition offsetGridPosition = new GridPosition(x, z);
GridPosition testGridPosition = unitGridPosition + offsetGridPosition;
if (!LevelGrid.Instance.IsValidGridPosition(testGridPosition))
{
continue;
}
if (unitGridPosition == testGridPosition)
{
//misma posicion del grid donde la unidad ya está
continue;
}
if (LevelGrid.Instance.HasAnyUnitOnGridPosition(testGridPosition))
{
//posicion del grid ya ocupada por otra unidad
continue;
}
if (!Pathfinding.Instance.IsWalkableGridPosition(testGridPosition))
{
continue;
}
if (!Pathfinding.Instance.HasPath(unitGridPosition, testGridPosition))
{
continue;
}
int pathfindingDistanceMulitplier = 10;
if (Pathfinding.Instance.GetPathLength(unitGridPosition, testGridPosition) >
maxMoveDistance * pathfindingDistanceMulitplier)
{
continue;
}
validGridPositionList.Add(testGridPosition);
}
}
return validGridPositionList;
}
public override string GetActionName()
{
return "Move";
}
// private void Move(Vector3 targetPosition)
// {
// this.targetPosition = targetPosition;
//}
public override EnemyAIAction GetEnemyAIAction(GridPosition gridPosition)
{
int targetCountAtGridPosirion = unit.GetAction<ShootAction>().GetTargetCountAtPosition(gridPosition);
return new EnemyAIAction
{
gridPosition = gridPosition,
actionValue = targetCountAtGridPosirion * 10,
};
}
}
I comment private void Move(Vector3 target position) because when i was making the script this cause some error, hopefully you can help me, thank you!!.
Are you getting available tiles to move to when the Move action is selected?
If so, what happens when you click on a valid tile location?
Nop, when i select the move action the tiles are not available and they don´t show up in the game.
If i use the next lines i got this error in console about not having a reference for the target position, but i check other topics with pathfinfing and move issues and they dont use this neither :
// private void Move(Vector3 targetPosition)
// {
// this.targetPosition = targetPosition;
//}
The Move() method was a temporary thing, and can safely be discarded.
Quick check: Look in PathNode.cs, and make sure that isWalkable is being defined as true by default.
Let’s do a couple of debugs to see what might be going on:
public void SetUp(int width, int height, float cellSize)
{
this.width = width;
this.height = height;
this.cellSize = cellSize;
gridSystem = new GridSystem<PathNode>(width, height, cellSize,
(GridSystem<PathNode> g, GridPosition gridPosition) => new PathNode(gridPosition));
//gridSystem.CreateDebugObjects(gridDebugObjectPrefab);
for (int x = 0; x < width; x++)
{
for (int z = 0; z < height; z++)
{
GridPosition gridPosition = new GridPosition(x, z);
Vector3 worldPosition = LevelGrid.Instance.GetWorldPosition(gridPosition);
float raycastOffsetDistance = 5f;
if (Physics.Raycast(
worldPosition + Vector3.down * raycastOffsetDistance,
Vector3.up,
raycastOffsetDistance * 2,
obstaclesLayerMask))
{
GetNode(x, z).SetIsWalkable(false);
}
Debug.Log($"Pathfinding.Setup - {gridPosition} walkable = {GetNode(x,z).IsWalkable()}");
}
}
}
This Debug should be showing the GridPositions of all the tiles in the game with walkable=true or walkable = false.
private List<GridPosition> CalculatePath(PathNode endNode)
{
List<PathNode> pathNodeList = new List<PathNode>();
pathNodeList.Add(endNode);
PathNode currentNode = endNode;
while (currentNode.GetCameFromPathNode() != null)
{
pathNodeList.Add(currentNode.GetCameFromPathNode());
currentNode = currentNode.GetCameFromPathNode();
}
pathNodeList.Reverse();
List<GridPosition> gridPositionList = new List<GridPosition>();
foreach (PathNode pathNode in pathNodeList)
{
Debug.Log($"Pathfinding.CalculatePath -- Adding {pathNode.GetGridPosition()}");
gridPositionList.Add(pathNode.GetGridPosition());
}
return gridPositionList;
}
These Debugs should be showing nodes beng tested and show debugs adding to paths when the Move Action is selected.
Done, the problem was the PathNode script, i was checking other scripts and i saw the SetHcost on the Path Node was empty, i wasn´t checking this right and was the trouble maker, thank you so much for help, you are doing a great job in this comunnity, thanks again!.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.