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

4

1 回答 1

0

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

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

然后在这个脚本中做任何你想做的事情。将此脚本附加到没有触发器的对象。通常,如果您有一名球员撞到球,比如说,当他击球时就会消失。您将希望球成为触发器,并希望玩家使用上述方法来隐藏球。附言。obj 将是触发对象。所以去 Destroy(obj); 会破坏球而不是球员。如果连接到播放器。

然后,如果您想要接近,您可以更改播放器上对撞机的大小。GL 你会弄明白的。

于 2014-03-26T12:47:08.527 回答