0

Unity で近接検出器を作成しようとしていますが、ちょっとした課題に直面しています。私のコードはC#です。<>

私のゲームでは、目的は都市から都市へと飛ぶことです。プレイヤーのヘリコプターがターゲット キューブ (都市を表す) に飛び込みます。そのトリガーイベントは正常に機能します。衝突時に、ターゲット キューブ ゲームオブジェクト (都市) が非アクティブ化され、新しいターゲット キューブ (都市) がマップ上の別の場所にスポーンされます。

これはすべて、以下のスクリプトで正常に機能しますが、もっとやりがいのあるものにしたいと考えています。プレイヤー ゲームオブジェクト (ヘリコプター) がターゲット キューブ ゲームオブジェクト (都市) の近くにある場合にのみ、ターゲット キューブ ゲームオブジェクト (都市) がスポーンするようにします。

スクリプトのどこで問題が発生しているかについては、以下のコード コメントを参照してください。

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 オブジェクト内の子として空のゲーム オブジェクトを作成しました。子には Rigidbody と Boxcollider があります。子の名前は ProxDect です。特定のボックス コライダに OntriggerEnter を指定するにはどうすればよいですか? そして、それをスクリプトのこの部分で if ステートメントとして使用することはできますか? それとも、あまりにも多くのものを 1 つの関数に結合しようとしていますか?

4

1 に答える 1