光线投射问题统一

本文关键字:问题 光线投射 | 更新日期: 2023-09-27 18:32:29

我需要读取玩家汽车对阵 2 辆 AI 汽车的第一、第二或第三的天气,我试图使用光线投射来实现这一点,但无济于事。我定期在关卡中设置航点,并有一个系统,当AI或玩家进入新航点时,它会将光线绘制到下一个检查点,以便我可以使用hit.distance计算其距离,然后将每次更新的距离分配给变量浮点数。但是,它没有获得正确的顺序,当我第一次出现时,它会在调试中显示所有可能的组合。我认为主要问题出现在我调试距离时,我就在航点旁边,它可能说 300,或者它可能说 0.003447 或其他什么,这真的让我感到困惑。

以下是投射光线的脚本:

    using UnityEngine;
using System.Collections;
public class RaceManager : MonoBehaviour {
    public GameObject seeking;
    public float distance1;
    public float distance2;
    public float distance3;
    public int playerPos;
    void Update (){
        Debug.Log (playerPos);
        //Firing Rays to the target every update and recording its distance.
        if (this.gameObject.name == "Player") {
            RaycastHit hit;
            Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
            if (Physics.Raycast (playerRay, out hit)) {
                distance1 = hit.distance;
                Debug.DrawLine (this.transform.position, seeking.transform.position);
            }
        }
        if (this.gameObject.name == "AICar1"){
        RaycastHit hit;
        Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
        if (Physics.Raycast (playerRay, out hit)){
                distance2 = hit.distance;
                Debug.DrawLine(this.transform.position, seeking.transform.position);
        }
    }
        if (this.gameObject.name == "AICar2") {
        RaycastHit hit;
        Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
            if (Physics.Raycast (playerRay, out hit)){
                distance3 = hit.distance;
                Debug.DrawLine(this.transform.position, seeking.transform.position);
            }
        }
        //Decide placement based on Ray Length.
        //If Player is first.
        if (distance1 < distance2 && distance1 < distance3) {
                playerPos = 1;
            }
        else if (distance1 < distance2 && distance1 > distance3 || distance1 > distance2 && distance1 < distance3) {
                playerPos = 2;
        }
        else {
                playerPos = 3;
        }
    }
}

这是负责在玩家或AI到达目标时更改目标的脚本

    using UnityEngine;
using System.Collections;
public class RaceManager : MonoBehaviour {
    public GameObject seeking;
    public float distance1;
    public float distance2;
    public float distance3;
    public int playerPos;
    void Update (){
        Debug.Log (playerPos);
        //Firing Rays to the target every update and recording its distance.
        if (this.gameObject.name == "Player") {
            RaycastHit hit;
            Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
            if (Physics.Raycast (playerRay, out hit)) {
                distance1 = hit.distance;
                Debug.DrawLine (this.transform.position, seeking.transform.position);
            }
        }
        if (this.gameObject.name == "AICar1"){
        RaycastHit hit;
        Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
        if (Physics.Raycast (playerRay, out hit)){
                distance2 = hit.distance;
                Debug.DrawLine(this.transform.position, seeking.transform.position);
        }
    }
        if (this.gameObject.name == "AICar2") {
        RaycastHit hit;
        Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
            if (Physics.Raycast (playerRay, out hit)){
                distance3 = hit.distance;
                Debug.DrawLine(this.transform.position, seeking.transform.position);
            }
        }
        //Decide placement based on Ray Length.
        //If Player is first.
        //All signs reversed for testing
        if (distance1 < distance2 && distance1 < distance3) {
                playerPos = 1;
            }
        else if (distance1 < distance2 && distance1 > distance3 || distance1 > distance2 && distance1 < distance3) {
                playerPos = 2;
        }
        else {
                playerPos = 3;
        }
    }
}

光线投射问题统一

如果您知道感兴趣航点的位置以及环境中所有车辆的位置,那么只需使用 Vector3 类 Vector3.Distance 中的静态距离函数。只需存储对车辆变换的引用,然后测量每辆车与相关航点之间的距离。使用光线投射方法执行您想做的事情的开销要大得多。