[英]Unity3D movement issue (grid-based pathfinding)
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;
Pathfinding 脚本没有任何问题,因为当我使用
public Transform character;
in the pathfinding script with an Update() FindPath(character.position, target)
.在带有
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):以防万一,如果您需要 Pathfinding 代码的参考(Sebastian Lague 教程):
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.您必须在使用 Vector3[] 路径数组之前对其进行实例化。 So in Awake(), just instantiate it with:
所以在 Awake() 中,只需将其实例化为:
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.如果您不确定数组中对象的长度或数量经常变化,最好使用 List 而不是数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.