- Home /
A* Pathfinding issues, player node is always off (converting 3d to 2d)
so I am using Sebastian Lague's second video on A* pathfinding (https://www.youtube.com/watch?v=nhiFx28e7JY&list=PLFt_AvWsXl0cq5Umv3pMC9SPnKjfp9eGW∈dex=2), and since he is using a 3d model, and I am using a 2d model, I need to convert some of the values to 2d. It is working fine, but the problem I ran into is that the node that detects the player is always off.
Like in here, the cyan node is the node where my player is supposed to be, and the my cursor is where my player actually is. You can see that it is completely off. I am pretty sure this is because some error is produced while I tried to turn 3d into 2d, but I don't know how. Here is my grid script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grid : MonoBehaviour
{
public Transform player;
public LayerMask unwalkableMask;
public Vector2 gridWorldSize;
public float nodeRadius;
Node[,] grid;
float nodeDiameter;
int gridSizeX, gridSizeY;
void Start() {
nodeDiameter = nodeRadius*2;
gridSizeX = Mathf.RoundToInt(gridWorldSize.x/nodeDiameter);
gridSizeY = Mathf.RoundToInt(gridWorldSize.y/nodeDiameter);
CreateGrid();
}
void CreateGrid() {
grid = new Node[gridSizeX,gridSizeY];
Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x/2 - Vector3.up * gridWorldSize.y/2;
for (int x = 0; x < gridSizeX; x ++) {
for (int y = 0; y < gridSizeY; y ++) {
Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.up * (y * nodeDiameter + nodeRadius);
bool walkable = !(Physics2D.OverlapCircle(worldPoint,nodeRadius,unwalkableMask));
grid[x,y] = new Node(walkable,worldPoint);
}
}
}
public Node NodeFromWorldPoint(Vector3 worldPosition) {
float percentX = (worldPosition.x + gridWorldSize.x/2) / gridWorldSize.x;
float percentY = (worldPosition.y + gridWorldSize.y/2) / gridWorldSize.y;
percentX = Mathf.Clamp01(percentX);
percentY = Mathf.Clamp01(percentY);
int x = Mathf.RoundToInt((gridSizeX-1) * percentX);
int y = Mathf.RoundToInt((gridSizeY-1) * percentY);
return grid[x,y];
}
void OnDrawGizmos()
{
Gizmos.DrawWireCube(transform.position, new Vector3(gridWorldSize.x, gridWorldSize.y, 1));
if (grid != null)
{
Node playerNode = NodeFromWorldPoint(player.position);
foreach (Node n in grid) {
Gizmos.color = (n.walkable)?Color.white:Color.red;
if (playerNode == n)
{
Gizmos.color = Color.cyan;
}
Gizmos.DrawCube(n.worldPosition, Vector3.one * (nodeDiameter-.1f));
}
}
}
}
and this is my node script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Node
{
public bool walkable;
public Vector3 worldPosition;
public Node(bool _walkable, Vector3 _worldPos) {
walkable = _walkable;
worldPosition = _worldPos;
}
}
Your answer
Follow this Question
Related Questions
AI & pathfinding for 2D platformer - how to approach? 0 Answers
Ways to find bounds of the platform 1 Answer
How do I implement A* pathfinding to my 2d game, without tiles? 4 Answers
adding things within certain distance into list 1 Answer
A* Pathfinding, destroying a path once it has reached its end 0 Answers