我需要阅读玩家汽车在2辆AI汽车上获得第1,第2或第3天气的天气,我试图使用光线投射来实现这一点无济于事。我定期在关卡中设置航点,并设置一个系统,当AI或玩家进入时设置新的航点,然后它将光线绘制到下一个检查点,以便我可以使用hit.distance计算其距离,然后分配该距离对变量float的每次更新。然而,它没有得到正确的顺序,当我第一次出现时,它会在调试中显示所有可能的组合。 我想到的主要问题是当我调试距离时,我就在航点旁边,它可能会说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;
}
}
}
答案 0 :(得分:2)
如果您知道感兴趣的航点的位置以及环境中所有车辆的位置,那么只需使用Vector3班Vector3.Distance中的静态距离函数。只需隐藏对车辆的引用即可。变换,然后测量每辆车与相关航路点之间的距离。使用光线投射方法来做你想做的事情会有更多的开销。