我想在Unity中創建接近檢測器,但是面臨一些挑戰。我的代碼是C#。 <>Unity中的接近檢測器
在我的遊戲中,目標是從城市飛往城市。玩家直升機飛入目標立方體(代表城市)。該觸發事件工作正常。在collisison中,目標立方體gameobject(城市)被停用,並且新的目標立方體(城市)將在地圖上的其他地方生成。
這一切都在下面的腳本中正常工作,但我想使它更具挑戰性。我希望目標魔方gameobject(城市)只在玩家gameobject(直升機)處於目標魔方gameobject(城市)的附近時產卵。
請參閱下面的代碼註釋,瞭解腳本中的哪些地方有困難。
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對象中創建了一個空的gameobject作爲一個子對象。孩子有一個剛體和Boxcollider。該孩子被命名爲ProxDect。如何爲那個特定的盒子對撞機指定一個OntriggerEnter?我甚至可以在腳本的這一部分中使用它作爲n if語句嗎?或者我想在一個功能中結合太多東西?
所以你想要一段代碼只在你的播放器在觸發器內時才能執行? – SirBraneDamuj
是的,抱歉,如果我的問題有點不清楚。我編輯了上面的問題來澄清。 – Killbert