유니티에 근접 감지기를 만들려고 노력하고 있지만 약간의 어려움에 직면하고 있습니다. 내 코드는 C#입니다. <>유니티의 근접 감지기
내 게임에서 목표는 도시에서 도시로 날아 오는 것입니다. 플레이어 헬리콥터가 대상 큐브 (도시를 대표 함)로 날아갑니다. 그 방아쇠 이벤트가 잘 작동합니다. collisison에서 대상 큐브 gameobject (도시)가 비활성화되고 새 대상 큐브 (도시)가지도의 다른 위치에서 스폰됩니다.
아래의 스크립트는 모두 정상적으로 작동하지만 더 어렵게 만들고 싶습니다. gameobject (helicopter) 플레이어가 대상 큐브 gameobject (city)의 부근에있는 경우에만 대상 큐브 gameobject (city)가 스폰되기를 원합니다.
내 스크립트의 어디에서 문제가 발생하는지 아래의 코드 주석을 참조하십시오.
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();
}
}
어떻게하면됩니까? 나는 빈 PlayerObject를 내 Player 객체 내부의 자식으로 만들었습니다. 그 아이는 리지드 바디와 박스 콜 라이더를 가지고 있습니다. 아이의 이름은 ProxDect입니다. 특정 상자 콜 리더에 대해 OntriggerEnter를 어떻게 지정합니까? 그리고이 스크립트 부분에서 n if 문으로 사용할 수 있습니까? 아니면 하나의 기능으로 너무 많은 것을 결합하려고합니까?
그래서 플레이어가 트리거 중에 만 특정 코드 블록을 실행하기를 원하십니까? – SirBraneDamuj
예, 제 질문에 약간 불명 한 점은 죄송합니다. 나는 위의 질문을 명확히하기 위해 편집했다. – Killbert