I'm currently trying to move my character to my mouse target. I did a lot of research since 2 weeks and I feel close to my goal but I'm still struggling. Before pressing play I get this warning:
Assets\Scripts\Worldmaps\Movement.cs(11,12): warning CS0649: Field 'Movement.path' is never assigned to, and will always have its default value null
And when I press play, after clicking on any node I get this error:
NullReferenceException: Object reference not set to an instance of an object
Movement.Update () (at Assets/Scripts/Worldmaps/Movement.cs:24)
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
private const float speed = 10f;
private int targetIndex;
Vector3[] path;
private Pathfinding pathfinding;
void Awake()
{
pathfinding = GetComponent<Pathfinding>();
}
private void Update()
{
if(Input.GetMouseButtonDown(0))
{
Vector3 target = GetMouseWorldPosition();
pathfinding.StartFindPath(transform.position, target);
targetIndex = 0;
Vector3 finalPath = path[0];
while(true)
{
if(transform.position == finalPath)
{
targetIndex++;
if(targetIndex >= path.Length)
{
break;
}
finalPath = path[targetIndex];
}
transform.position = Vector3.MoveTowards(transform.position, finalPath, speed * Time.deltaTime);
}
Debug.Log(target);
}
}
public static Vector3 GetMouseWorldPosition()
{
Vector3 clickPosition = new Vector3();
clickPosition.y = 0;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
clickPosition = hit.point;
}
return clickPosition;
}
}
The Pathfinding script doesn't have any issue as it finds and update very well the path between the character and the target when I use a public Transform character;
in the pathfinding script with an Update() FindPath(character.position, target)
.
Even if I'm a beginner and lack the skill, I know coding for the others may be annoying so honestly advices are welcome too! Pathfinding is probably not the easiest thing to start as a beginner but this is the base of a project I really want to continue!
Just in case, if you need the reference of the Pathfinding code (Sebastian Lague tutorial):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Pathfinding : MonoBehaviour
{
AGrid grid;
void Awake()
{
grid = GetComponent<AGrid>();
}
public void StartFindPath(Vector3 startPos, Vector3 targetPos)
{
StartCoroutine(FindPath(startPos, targetPos));
}
IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
{
Node startNode = grid.NodeFromWorldPoint(startPos);
Node targetNode = grid.NodeFromWorldPoint(targetPos);
Heap<Node> openSet = new Heap<Node>(grid.MaxSize);
HashSet<Node> closedSet = new HashSet<Node>();
openSet.Add(startNode);
while(openSet.Count > 0)
{
Node currentNode = openSet.RemoveFirst();
closedSet.Add(currentNode);
if(currentNode == targetNode)
{
RetracePath(startNode, targetNode);
break;
}
foreach(Node neighbour in grid.GetNeighbours(currentNode))
{
if(!neighbour.walkable || closedSet.Contains(neighbour))
{
continue;
}
int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);
if(newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
{
neighbour.gCost = newMovementCostToNeighbour;
neighbour.hCost = GetDistance(neighbour, targetNode);
neighbour.parent = currentNode;
if(!openSet.Contains(neighbour))
{
openSet.Add(neighbour);
}
else
{
openSet.UpdateItem(neighbour);
}
}
}
}
yield return null;
}
void RetracePath(Node startNode, Node endNode)
{
List<Node> path = new List<Node>();
Node currentNode = endNode;
while(currentNode != startNode)
{
path.Add(currentNode);
currentNode = currentNode.parent;
}
path.Add(startNode);
path.Reverse();
}
int GetDistance(Node nodeA, Node nodeB)
{
int dstX = Mathf.Abs(nodeA.gridX - nodeB.gridX);
int dstZ = Mathf.Abs(nodeA.gridZ - nodeB.gridZ);
if(dstX > dstZ)
{
return 14 * dstZ + 10 * (dstX - dstZ);
}
return 14 * dstX + 10 * (dstZ - dstX);
}
}
Thank you in advance!
Flowergun :)
You have to instantiate the Vector3[] path array before using it. So in Awake(), just instantiate it with:
path = new Vector3[<write the length of the array here>];
As an example:
path = new Vector3[5];
If you are unsure about the length or the number of objects in the array changes often, it would be better to use a List instead of an array.