繁体   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