让玩家跟随路径点但仍能控制玩家移动

我正在开发一款赛车游戏,我设定了一条带有路径点的预定义路径。我希望玩家能够沿着这条路径的大致方向前进,同时我仍然能够完全控制玩家的移动。也就是说,玩家会继续沿着路径前进,但我应该能够左右移动玩家以避开障碍物。

这是我目前使用的代码片段。

#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

但问题在于,即使我左右移动玩家,玩家仍然会直接前往路径点。它会回到路径点的中心。请观看下面的GIF图:-

Demo Gif

如您所见,玩家会回到中心以跟随路径点,我无法以这种方式控制玩家。我只希望玩家能够沿着赛道路径前进,但我想控制其X轴位置以避开障碍物。

能有人在这里指导我并帮助我解决这个问题吗?任何帮助或建议都将不胜感激。

谢谢。


回答:

下面的方法可能不是解决这个问题的完美方案,但可以帮助进一步解决问题

您可以按以下方式设置玩家

带有脚本的玩家对象和作为子对象的角色(胶囊体),请查看下面的图片

player_heirarchy

然后使用下面的代码,您将能够让角色侧向移动,同时让主对象跟随路径点。之后您可以添加一些限制来控制最大侧向移动距离。

void Update(){    //根据您的代码创建了一个类似的情境    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++;        }    }    //对子对象进行侧向移动,在我的例子中是玩家对象的第二个子对象    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

附注:这是我的第一个回答,如果格式不正确,请原谅。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注