如何在触发时加载预制件以便我的游戏继续进行?
How can I load in a prefab on trigger so my game keeps going?
我正在 Unity 中制作一个 'run' 游戏,我正在制作一个带有球的原型,其他球会跟随他。如果追随者击中一个物体,他们会在一段时间后被摧毁。我做了一条带有障碍物的道路并预制了它们。现在我的问题是如何在触发时加载预制件或其他东西,以便我的游戏继续进行。目标是我不需要制作完整的地图,而是从不同道路的预制件中随机选择地图。
如果这对我的主角有帮助,我想也许它在另一个触发器中加载了预制件或其他东西?
这是我的代码。
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
public float InputForce;
public GUIText guiText;
public float rotationHorizontal;
public AudioClip ACeffect2;
public GameObject zombiePrefab;
void FixedUpdate() {
rigidbody.AddForce( Camera.main.transform.right * Input.GetAxis("Horizontal") * InputForce);
rigidbody.AddForce( Camera.main.transform.forward * Input.GetAxis("Vertical") * InputForce);
transform.position += Vector3.forward *InputForce * Time.deltaTime;
rotationHorizontal = Input.GetAxis("Horizontal") * InputForce;
rotationHorizontal *= Time.deltaTime;
rigidbody.AddRelativeTorque (Vector3.back * rotationHorizontal);
}
void OnCollisionEnter(Collision col){
if (col.gameObject.name == "Zombie") {
Debug.Log ("Player geraakt, nu ben je eigenlijk dood");
}
if (col.gameObject.name == "Obstakel1") {
Debug.Log ("Obstakel1 geraakt");
audio.PlayOneShot(ACeffect2);
InputForce = 0;
}
if (col.gameObject.name == "Obstakel2") {
Debug.Log ("Obstakel2 geraakt");
}
}
void AddZombies(int aantal){
for (int i = 0; i < aantal; i++){
GameObject go = GameObject.Instantiate(zombiePrefab, transform.position - new Vector3(0, 0, 7 + i),Quaternion.identity) as GameObject;
Zombie zb = go.GetComponent<Zombie>();
zb.target = gameObject.transform;
}
}
void OnTriggerEnter(Collider col) {
Debug.Log ("Enter" +col.name);
if (col.tag == "AddZombies"){
AddZombies(4);
}
}
void OnTriggerExit(Collider col) {
Debug.Log ("Leaving with" +col.name);
}
}
制作一个游戏对象并添加一个包含以下代码的组件:
public GameObject prefab; // Drag and drop prefab to component in unity
// When trigger is triggered
void OnTriggerEnter(Collider col)
{
Instantiate(prefab, new Vector3(0,0,0), Quaternion.Identity);
}
然后你可以将你想要实例化的prefab拖放到unity中的组件中。
该位置可以是您希望它生成的位置(例如 transform.position
使其在您放置触发器的位置生成)。
请注意,要制作触发器,您需要制作一个新的空游戏对象并添加您想要的任何碰撞器。要使其调用 OnTriggerEnter
方法,您还需要选中碰撞器组件上的框 is trigger
。
我正在 Unity 中制作一个 'run' 游戏,我正在制作一个带有球的原型,其他球会跟随他。如果追随者击中一个物体,他们会在一段时间后被摧毁。我做了一条带有障碍物的道路并预制了它们。现在我的问题是如何在触发时加载预制件或其他东西,以便我的游戏继续进行。目标是我不需要制作完整的地图,而是从不同道路的预制件中随机选择地图。
如果这对我的主角有帮助,我想也许它在另一个触发器中加载了预制件或其他东西?
这是我的代码。
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
public float InputForce;
public GUIText guiText;
public float rotationHorizontal;
public AudioClip ACeffect2;
public GameObject zombiePrefab;
void FixedUpdate() {
rigidbody.AddForce( Camera.main.transform.right * Input.GetAxis("Horizontal") * InputForce);
rigidbody.AddForce( Camera.main.transform.forward * Input.GetAxis("Vertical") * InputForce);
transform.position += Vector3.forward *InputForce * Time.deltaTime;
rotationHorizontal = Input.GetAxis("Horizontal") * InputForce;
rotationHorizontal *= Time.deltaTime;
rigidbody.AddRelativeTorque (Vector3.back * rotationHorizontal);
}
void OnCollisionEnter(Collision col){
if (col.gameObject.name == "Zombie") {
Debug.Log ("Player geraakt, nu ben je eigenlijk dood");
}
if (col.gameObject.name == "Obstakel1") {
Debug.Log ("Obstakel1 geraakt");
audio.PlayOneShot(ACeffect2);
InputForce = 0;
}
if (col.gameObject.name == "Obstakel2") {
Debug.Log ("Obstakel2 geraakt");
}
}
void AddZombies(int aantal){
for (int i = 0; i < aantal; i++){
GameObject go = GameObject.Instantiate(zombiePrefab, transform.position - new Vector3(0, 0, 7 + i),Quaternion.identity) as GameObject;
Zombie zb = go.GetComponent<Zombie>();
zb.target = gameObject.transform;
}
}
void OnTriggerEnter(Collider col) {
Debug.Log ("Enter" +col.name);
if (col.tag == "AddZombies"){
AddZombies(4);
}
}
void OnTriggerExit(Collider col) {
Debug.Log ("Leaving with" +col.name);
}
}
制作一个游戏对象并添加一个包含以下代码的组件:
public GameObject prefab; // Drag and drop prefab to component in unity
// When trigger is triggered
void OnTriggerEnter(Collider col)
{
Instantiate(prefab, new Vector3(0,0,0), Quaternion.Identity);
}
然后你可以将你想要实例化的prefab拖放到unity中的组件中。
该位置可以是您希望它生成的位置(例如 transform.position
使其在您放置触发器的位置生成)。
请注意,要制作触发器,您需要制作一个新的空游戏对象并添加您想要的任何碰撞器。要使其调用 OnTriggerEnter
方法,您还需要选中碰撞器组件上的框 is trigger
。