2014-03-25 54 views
0

我想在Unity中創建接近檢測器,但是面臨一些挑戰。我的代碼是C#。 <>Unity中的接近檢測器

在我的遊戲中,目標是從城市飛往城市。玩家直升機飛入目標立方體(代表城市)。該觸發事件工作正常。在collisison中,目標立方體gameobject(城市)被停用,並且新的目標立方體(城市)將在地圖上的其他地方生成。

這一切都在下面的腳本中正常工作,但我想使它更具挑戰性。我希望目標魔方gameobject(城市)只在玩家gameobject(直升機)處於目標魔方gameobject(城市)的附近時產卵。

請參閱下面的代碼註釋,瞭解腳本中的哪些地方有困難。

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

我該怎麼做?我在Player對象中創建了一個空的gameobject作爲一個子對象。孩子有一個剛體和Boxcollider。該孩子被命名爲ProxDect。如何爲那個特定的盒子對撞機指定一個OntriggerEnter?我甚至可以在腳本的這一部分中使用它作爲n if語句嗎?或者我想在一個功能中結合太多東西?

+0

所以你想要一段代碼只在你的播放器在觸發器內時才能執行? – SirBraneDamuj

+0

是的,抱歉,如果我的問題有點不清楚。我編輯了上面的問題來澄清。 – Killbert

回答

0

因此,在箱子colider組件中有一個小盒子說Trigger。檢查一下。然後,您希望前者碰撞的對象確保它們具有碰撞器並創建新腳本。添加此方法:

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

然後在此腳本中執行任何其他操作。將此腳本附加到沒有觸發器的對象。通常情況下,如果你有一名球員碰到一個球,讓我們說,當他擊中球時消失。您需要將球作爲觸發器,並在球員上方隱藏球。 ps。 obj會成爲觸發器對象。所以去Destroy(obj);會摧毀球而不是球員。如果連接到播放器。

然後,如果您想要接近度,您可以更改播放器上對撞機的大小。 GL 你會發現它。