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

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

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

#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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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