2014-03-25 2 views
0

유니티에 근접 감지기를 만들려고 노력하고 있지만 약간의 어려움에 직면하고 있습니다. 내 코드는 C#입니다. <>유니티의 근접 감지기

내 게임에서 목표는 도시에서 도시로 날아 오는 것입니다. 플레이어 헬리콥터가 대상 큐브 (도시를 대표 함)로 날아갑니다. 그 방아쇠 이벤트가 잘 작동합니다. collisison에서 대상 큐브 gameobject (도시)가 비활성화되고 새 대상 큐브 (도시)가지도의 다른 위치에서 스폰됩니다.

아래의 스크립트는 모두 정상적으로 작동하지만 더 어렵게 만들고 싶습니다. gameobject (helicopter) 플레이어가 대상 큐브 gameobject (city)의 부근에있는 경우에만 대상 큐브 gameobject (city)가 스폰되기를 원합니다.

내 스크립트의 어디에서 문제가 발생하는지 아래의 코드 주석을 참조하십시오.

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 

public class PlayerController : MonoBehaviour 
{ 
    public float speed; 
    public float smooth = 2.0F; 
    public GUIText countText; 
    public GUIText targetCity; 
    private int count; 
    public GameObject cityPrefab; 
    List<MyCity> mycities; 

    void Start() 
    { 
     mycities = new List<MyCity>(); 

     mycities.Add(new MyCity("Maastricht", 3.635356F, 0.6F, -22.77141F)); 
     mycities.Add(new MyCity("Breda", -6.013169F, 0.6F, -10.64377F)); 
     mycities.Add(new MyCity("Amsterdam", -4.768597F, 0.6F, 2.610285F)); 
     mycities.Add(new MyCity("Assen", 12.89446F, 0.6F, 13.20904F)); 
     mycities.Add(new MyCity("Groningen", 12.89446F, 0.6F, 17.18825F)); 
     mycities.Add(new MyCity("Utrecht", -2.468696F, 0.6F, -2.110941F)); 
     mycities.Add(new MyCity("Leeuwarden", 4.773322F, 0.6F, 16.8759F)); 
     mycities.Add(new MyCity("Nijmegen", 5.137639F, 0.6F, -6.816391F)); 
     mycities.Add(new MyCity("Rotterdam", -9.139206F, 0.6F, -4.898618F)); 
     mycities.Add(new MyCity("Zwolle", 7.717577F, 0.6F, 5.111133F)); 
     mycities.Add(new MyCity("Den Helder", -6.195175F, 0.6F, 12.58271F)); 
     mycities.Add(new MyCity("Eindhoven", 1.262518F, 0.6F, -13.01812F)); 
     mycities.Add(new MyCity("Den Haag", -11.0655F, 0.6F, -2.512064F)); 
     mycities.Add(new MyCity("Arnhem", 5.79248F, 0.6F, -3.911978F)); 

     // scoring points & display on screen (works) 
     count = 0; 
     SetCountText(); 
     SetTargetCity(); 
    } 

    void SetTargetCity() 
    { 
     var randomCity = mycities [Random.Range (0, mycities.Count - 1)]; 
     targetCity.text = "Fly to: " + randomCity.name.ToString(); 

     // HERE IS WHERE I NEED TO CREATE AN OnTriggerEnter function, so that the below line is only 
     // executed if my helicopter is in the vicinity of the cityPrefab gameobject. The problem is 
     // that further down in the script, I already use an OnTriggerEnter on the same object. 

     GameObject instancedCity = (GameObject)GameObject.Instantiate (cityPrefab); 
     instancedCity.transform.position = new Vector3 (randomCity.xcor, randomCity.ycor, randomCity.zcor); 
    } 

    // Player Movement (works) 
    void FixedUpdate() 
    { 
     float moveHorizontal = Input.GetAxis("Horizontal"); 
     float moveVertical = Input.GetAxis("Vertical"); 

     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); 

     Vector3 moveDirection= new Vector3 (moveHorizontal, 0, moveVertical); 
     if (moveDirection != Vector3.zero) { 
      Quaternion newRotation = Quaternion.LookRotation(moveDirection * -1); 
      transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * smooth); 

      rigidbody.AddForce(movement * speed * Time.deltaTime);    
     }   
    } 

    // This part below works fine, but only takes care of deactivating the city cube instance after 
    // the helicopter has flown into it.... 

    void OnTriggerEnter(Collider other) 
    { 
     if (other.gameObject.tag == "City") { 
      other.gameObject.SetActive (false); 

      count = count + 1; 

      SetCountText(); 
      SetTargetCity(); 
     } 
    } 

    void SetCountText() 
    { 
     countText.text = "Passengers Picked up: " + count.ToString(); 
    } 
} 

어떻게하면됩니까? 나는 빈 PlayerObject를 내 Player 객체 내부의 자식으로 만들었습니다. 그 아이는 리지드 바디와 박스 콜 라이더를 가지고 있습니다. 아이의 이름은 ProxDect입니다. 특정 상자 콜 리더에 대해 OntriggerEnter를 어떻게 지정합니까? 그리고이 스크립트 부분에서 n if 문으로 사용할 수 있습니까? 아니면 하나의 기능으로 너무 많은 것을 결합하려고합니까?

+0

그래서 플레이어가 트리거 중에 만 특정 코드 블록을 실행하기를 원하십니까? – SirBraneDamuj

+0

예, 제 질문에 약간 불명 한 점은 죄송합니다. 나는 위의 질문을 명확히하기 위해 편집했다. – Killbert

답변

0

그래서 상자 색상 구성 요소에는 트리거라는 작은 상자가 있습니다. 그것을 확인하십시오. 그런 다음 이전과 충돌시키려는 객체가 충돌자를 가지고 있는지 확인하고 새 스크립트를 만듭니다. 이 방법 추가 :

void OnTriggerEnter(Collider obj){ 
    print ("Object Hit"); 
} 

그런 다음이 스크립트에서 원하는 모든 작업을 수행하십시오. 이 스크립트를 트리거가없는 오브젝트에 첨부하십시오. 보통, 볼을하고있는 플레이어가 있다고하면, 볼을 치면 볼이 사라집니다. 플레이어가 볼을 숨길 수있는 방아쇠와 위의 방법을 원할 것입니다. ps. obj는 객체가됩니다. 이렇게가는 파괴 (obj); 플레이어가 아닌 공을 파괴 할 것입니다. 플레이어에 부착 된 경우.

근접성을 원할 경우 플레이어의 충돌기 크기를 변경할 수 있습니다. GL 당신은 그것을 알아낼 것입니다.