仅在射线未击中障碍物时才看向玩家

我有一个规避脚本,它可以正常工作,但唯一的问题是它总是在绕过障碍物时试图看向玩家,导致抖动。我想知道如何让它仅在射线未击中障碍物时才看向玩家,同时继续向玩家移动。任何帮助都将不胜感激,目前使用的是Unity3D

using UnityEngine;using System.Collections;public class enemyAI : MonoBehaviour{       public Transform target;    public float moveSpeed;    public float rotationSpeed;    public float minDistance = 0.5f;    public static enemyAI enemyAIself;     RaycastHit hit;    void Awake()    {       enemyAIself = this;       }    void Start ()     {       GameObject goTo = GameObject.FindGameObjectWithTag("Player");       target = goTo.transform;    }    void Update ()     {       Vector3 dir = (target.position - transform.position).normalized;       if (Physics.Raycast(transform.position, transform.forward, out hit, 5.0f, (1<<8)))       {       Debug.DrawRay(transform.position, hit.point, Color.blue);       dir += hit.normal  * 50;       }       Vector3 leftR = transform.position;       Vector3 rightR = transform.position;       leftR.x -= 2;       rightR.x += 2;       if (Physics.Raycast(leftR, transform.forward, out hit, 5.0f, (1<<8)))       {        Debug.DrawRay(leftR, hit.point, Color.blue);          dir += hit.normal  * 50;       }       if (Physics.Raycast(rightR, transform.forward, 5.0f, (1<<8)))       {       Debug.DrawRay(rightR, hit.point, Color.blue);       dir += hit.normal  * 50;       }        Quaternion rot = Quaternion.LookRotation(dir);       transform.rotation = Quaternion.Slerp(transform.rotation, rot, rotationSpeed * Time.deltaTime);       if (Vector3.SqrMagnitude(target.position - transform.position)> (minDistance *  minDistance))       {         //move towards the target         transform.position += transform.forward * moveSpeed * Time.deltaTime;       }    }}

回答:

要让它仅在射线未击中障碍物时才看向玩家,同时继续向玩家移动,你可以

在击中点创建一个临时路径点(Vector3)

沿着障碍物移动路径点,直到从该点发出的射线不再击中障碍物

移动到这个路径点

继续向玩家前进

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中创建了一个多类分类项目。该项目可以对…

发表回复

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