Unity中的接近检测器

时间:2014-03-25 18:28:48

标签: c# unity3d proximitysensor

我正在尝试在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对象中创建了一个空的游戏对象作为孩子。孩子有一个Rigidbody和Boxcollider。这个孩子被命名为ProxDect。如何为该特定的盒式对撞机指定OntriggerEnter?我甚至可以在脚本的这一部分中使用它作为if语句吗?或者我想在一个函数中组合太多东西?

1 个答案:

答案 0 :(得分:0)

因此,在盒子colider组件中有一个小盒子,上面写着Trigger。检查一下。然后,您希望前者碰撞的对象确保它们具有碰撞器并创建新脚本。添加此方法:

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

然后在此脚本中执行您想要的任何其他操作。将此脚本附加到没有触发器的对象。通常情况下,如果你有一个球员碰到一个球,让我们说,当他击球时,球就会消失。你需要将球作为触发器和球员上方的方法来隐藏球。 PS。 obj将成为触发对象。所以去毁灭(obj);会破坏球而不是球员。如果附在播放器上。

然后,如果你想要接近,你可以改变玩家对撞机的大小。 GL 你会明白的。