阻止射线检测穿过墙壁

我正在制作一个游戏,玩家需要能够躲避敌人。当敌人看到我时,它会追随我,但当我在我们之间放置一堵墙时,射线检测会忽略这堵墙,导致敌人仍然继续追随我。我没有使用层,因为我不太理解它们的作用。甚至我也不知道是否应该使用它们。以下是我的敌人AI脚本,用C#编写:

using UnityEngine; using System.Collections;public class EnemyAI : MonoBehaviour { private GameObject playerTarget;private SphereCollider col;public Transform target;public int moveSpeed;public int rotationSpeed;public int maxDistance;private Transform myTransform;public int RayDist;public Vector3 position;public float RayDir;void Start (){    playerTarget = GameObject.FindGameObjectWithTag ("Player");    col = GetComponent<SphereCollider>();    myTransform = transform;    GameObject go = GameObject.FindGameObjectWithTag("Player");    character = GetComponent<CharacterController>();    target = go.transform;      var diagonal = transform.TransformDirection(1, 0, 1);     position = transform.position-transform.right*3;}CharacterController character;void Update(){}void OnTriggerStay (Collider other){    if(other.gameObject == playerTarget)    {           RaycastHit hit;        Vector3 dir = target.position - myTransform.position; dir.y = 0;         Vector3 fwd = transform.TransformDirection(Vector3.forward * RayDist);        if(Physics.Raycast(transform.position, fwd, out hit,10))        {            if (hit.transform.gameObject == playerTarget)            {            Debug.Log ("Raycast entered ");            myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);             if (dir.magnitude > maxDistance)             {                 character.Move(myTransform.forward * moveSpeed * Time.deltaTime);             }             }        }        Debug.DrawRay (transform.position, fwd, Color.green);        Vector3 slightright = transform.TransformDirection(new Vector3(RayDir,0,2) * RayDist);        if(Physics.Raycast (transform.position, slightright, out hit, 10)){            if (hit.transform.gameObject == playerTarget)            {            Debug.Log ("slightright Raycast entered");            myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);             if (dir.magnitude > maxDistance)             {                 Debug.Log ("Left Raycast move function entered");                character.Move(myTransform.forward * moveSpeed * Time.deltaTime);             }         }        }        Debug.DrawRay (transform.position, slightright, Color.magenta);        Vector3 right = transform.TransformDirection(new Vector3(RayDir,0,1) * RayDist);        if(Physics.Raycast (transform.position, right, out hit, 10)){            if (hit.transform.gameObject == playerTarget)            {            Debug.Log ("Right Raycast entered");            myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);             if (dir.magnitude > maxDistance)             {                 character.Move(myTransform.forward * moveSpeed * Time.deltaTime);             }         }        }        Debug.DrawRay (transform.position, right, Color.blue);        Vector3 left = transform.TransformDirection(new Vector3(-RayDir,0,1) * RayDist);        if(Physics.Raycast (transform.position, left, out hit, 10)){            if (hit.transform.gameObject == playerTarget)            {            Debug.Log ("Left Raycast entered");            myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);             if (dir.magnitude > maxDistance)             {                 Debug.Log ("Left Raycast move function entered");                character.Move(myTransform.forward * moveSpeed * Time.deltaTime);             }         }        }        Debug.DrawRay (transform.position, left, Color.red);        Vector3 slightleft = transform.TransformDirection(new Vector3(-RayDir,0,2) * RayDist);        if(Physics.Raycast (transform.position, slightleft, out hit, 10)){            if (hit.transform.gameObject == playerTarget)            {            Debug.Log ("Left Raycast entered");            myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);             if (dir.magnitude > maxDistance)             {                 Debug.Log ("Left Raycast move function entered");                character.Move(myTransform.forward * moveSpeed * Time.deltaTime);             }         }        }        Debug.DrawRay (transform.position, slightleft, Color.cyan);    }}

}


回答:

你没有检查射线检测击中的对象是否真的是玩家。所以你的代码基本上是说:“如果这个射线检测击中了任何东西,就跟随玩家。”要检查射线检测击中的是哪个对象,只需创建一个RaycastHit对象,并将其作为参数添加到Physics.Raycast调用中。所以你的第一个射线检测应该看起来像这样:

RaycastHit hit;if(Physics.Raycast(transform.position, fwd, out hit ,10)){    if (hit.transform.gameObject == playerTarget)    {        Debug.Log ("Raycast entered ");        myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);         if (dir.magnitude > maxDistance)         {             character.Move(myTransform.forward * moveSpeed * Time.deltaTime);         }    }}

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

发表回复

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