2014-11-29 5 views
1

날씨가 좋을 때, 2 대의 인공 지능 자동차에 대해 1, 2, 3 등으로 플레이어의 차가오고 있습니다. 레이 캐스팅을 사용하여이를 수행하려고했습니다. 나는 레벨에서 웨이 포인트를 주기적으로 가지고 있으며 AI 나 플레이어가 그것을 입력 할 때 새로운 웨이 포인트를 설정하는 시스템을 가지고 있습니다. 그러면 다음 체크 포인트로 광선을 그어 hit.distance를 사용하여 거리를 계산 한 다음 그 거리를 할당합니다 변수 float에 대한 모든 업데이트 그러나 주문이 제대로되지 않고 있으며, 먼저 디버깅 할 때 모든 가능한 조합을 표시합니다. 거리를 디버깅 할 때 가장 중요한 문제는 중간 지점 바로 옆에 있으며 300을 말할 수도 있고 0.003447 또는 무엇인가 말할 수있어 정말 혼란 스럽습니다. 여기Raycasting Issue Unity

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; 
     } 
    } 
} 

답변

2

에 도착하면 스크립트가 대상을 변경하는 책임이있다 : 아래

는 광선을 캐스팅하기위한 스크립트입니다 관심있는 웨이 포인트와 환경 내의 모든 차량의 위치를 ​​알고 싶다면 Vector3 클래스의 정적 거리 함수 Vector3.Distance을 사용하면됩니다. 차량의 변형에 대한 참조를 숨긴 다음 각 차량과 관련 중간 지점 사이의 거리를 측정합니다. Ray Casting 메서드를 사용하여 수행하려는 작업을 수행하는 데 더 많은 오버 헤드가 있습니다.

+1

감사합니다. 불편을 끼쳐 드려 – Luke

+0

감사합니다. 내가 할 수 있다는 것을 결코 알지 못했다. 그러나 그것은 효과가 있었다. 당신의 대답을 옳다고 표시했습니다! : D – Luke

+0

위대한 :-) 내가 도울 수있어서 기쁩니다. –