簡體   English   中英

Unity中的接近檢測器

[英]Proximity detector in Unity

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

在我的游戲中,目標是從一個城市飛到另一個城市。 玩家直升機飛入目標立方體(代表城市)。 該觸發事件可以正常工作。 在collisison上,將停用目標多維數據集游戲對象(城市),並在地圖上的其他位置產生一個新的目標多維數據集(城市)。

所有這些在下面的腳本中都可以正常運行,但是我想使其更具挑戰性。 我希望僅在玩家游戲對象(直升機)在目標立方體游戲對象(城市) 附近時才生成目標立方體游戲對象(城市)。

有關我在腳本中遇到的困難,請參見下面的代碼注釋。

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對象中,我還是個孩子的時候就做了一個空的游戲對象。 這個孩子有一個剛體和Boxcollider。 該子名為ProxDect。 如何為該特定的對撞機指定OntriggerEnter? 我甚至可以在腳本的這一部分中將其用作if語句嗎? 還是我要在一個函數中組合太多東西?

因此,在大腸菌箱中有一個小盒子,上面寫着扳機。 檢查一下。 然后,要讓前者與之碰撞的對象確保它們具有碰撞器並創建一個新腳本。 添加此方法:

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

然后執行此腳本中您想要執行的其他任何操作。 將此腳本附加到沒有觸發器的對象。 通常,如果您有球員撞到球,可以說,當他擊球時,球就會消失。 您將希望球成為觸發器,並且希望玩家使用上面的方法隱藏球。 ps。 obj將是觸發器對象。 因此要銷毀(obj); 會破壞球而不是球員。 如果連接到播放器。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM