让玩家跟随一个航点路径,但仍然能够控制玩家的移动
Make player follow a waypoint path but still be able to control player movement
我正在开发一款赛车游戏,其中我有一个预定义的路径 waypoints,我想要实现的是,我希望我的玩家遵循路径的总体方向,但仍处于完全控制之中我的播放器。就像它一直沿着路径走,但我应该能够左右移动它来避开障碍物。
这是我现在使用的一段代码。
#region Following the path
if (PathGenerator.instance.wayPoints.Count > 0)
{
Transform currentNode = PathGenerator.instance.wayPoints[currentNodeCount];
transform.position = Vector3.MoveTowards(transform.position, currentNode.position, forwardSpeed * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(currentNode.position - transform.position), 1 * Time.deltaTime);
float distance = Vector3.Distance(transform.position, currentNode.position);
if(distance < 1f)
{
if(currentNodeCount < PathGenerator.instance.wayPoints.Count - 1)
currentNodeCount++;
}
}
#endregion
但这里的问题是,即使我左右移动,玩家也会直接到达路径点。它只会回到航路点的中心。
观看动图:-
如你所见,它是如何回到中心跟随航路点的,我无法像这样控制播放器。我只想让玩家自己跟随电路路径,但我想控制它的 X 轴以避免障碍。
有人可以在这里指导我并帮助我解决问题吗?任何帮助或想法将不胜感激。
谢谢。
以下可能不是解决此问题的最佳方法,但应该有助于更进一步
您可以如下设置播放器
玩家 object 脚本和角色(胶囊)为 child
请检查下图
然后使用下面的代码,您将被允许将角色侧向移动,保持主要 object 跟随路径点。
稍后您可以为最大侧向移动添加一些限制。
void Update()
{
//made a similar situation from your code
if (waypoints.Length > 0)
{
Transform currentNode = waypoints[currentNodeCount].transform;
transform.position = Vector3.MoveTowards(transform.position, currentNode.position, 10f * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(currentNode.position - transform.position), 1 * Time.deltaTime);
float distance = Vector3.Distance(transform.position, currentNode.position);
if (distance < 1f)
{
if (currentNodeCount < waypoints.Length - 1)
currentNodeCount++;
}
}
// side movement to child object, in m case it is Player Object's second child
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.GetChild(1).Translate(Vector3.left * 5f * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.RightArrow))
{
transform.GetChild(1).Translate(Vector3.right * 5f * Time.deltaTime);
}
}
同时附上输出视频
Follow_waypoint_along_with_manual_side_movement
P.S。 : 第一次回答,格式不正确还请见谅
我正在开发一款赛车游戏,其中我有一个预定义的路径 waypoints,我想要实现的是,我希望我的玩家遵循路径的总体方向,但仍处于完全控制之中我的播放器。就像它一直沿着路径走,但我应该能够左右移动它来避开障碍物。
这是我现在使用的一段代码。
#region Following the path
if (PathGenerator.instance.wayPoints.Count > 0)
{
Transform currentNode = PathGenerator.instance.wayPoints[currentNodeCount];
transform.position = Vector3.MoveTowards(transform.position, currentNode.position, forwardSpeed * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(currentNode.position - transform.position), 1 * Time.deltaTime);
float distance = Vector3.Distance(transform.position, currentNode.position);
if(distance < 1f)
{
if(currentNodeCount < PathGenerator.instance.wayPoints.Count - 1)
currentNodeCount++;
}
}
#endregion
但这里的问题是,即使我左右移动,玩家也会直接到达路径点。它只会回到航路点的中心。 观看动图:-
如你所见,它是如何回到中心跟随航路点的,我无法像这样控制播放器。我只想让玩家自己跟随电路路径,但我想控制它的 X 轴以避免障碍。
有人可以在这里指导我并帮助我解决问题吗?任何帮助或想法将不胜感激。
谢谢。
以下可能不是解决此问题的最佳方法,但应该有助于更进一步
您可以如下设置播放器
玩家 object 脚本和角色(胶囊)为 child 请检查下图
然后使用下面的代码,您将被允许将角色侧向移动,保持主要 object 跟随路径点。 稍后您可以为最大侧向移动添加一些限制。
void Update()
{
//made a similar situation from your code
if (waypoints.Length > 0)
{
Transform currentNode = waypoints[currentNodeCount].transform;
transform.position = Vector3.MoveTowards(transform.position, currentNode.position, 10f * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(currentNode.position - transform.position), 1 * Time.deltaTime);
float distance = Vector3.Distance(transform.position, currentNode.position);
if (distance < 1f)
{
if (currentNodeCount < waypoints.Length - 1)
currentNodeCount++;
}
}
// side movement to child object, in m case it is Player Object's second child
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.GetChild(1).Translate(Vector3.left * 5f * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.RightArrow))
{
transform.GetChild(1).Translate(Vector3.right * 5f * Time.deltaTime);
}
}
同时附上输出视频 Follow_waypoint_along_with_manual_side_movement
P.S。 : 第一次回答,格式不正确还请见谅