1

私は Unity で「ラン」ゲームを作成しており、ボールを使ってプロトタイプを作成しています。このプロトタイプには、他のボールが続きます。フォロワーがオブジェクトにヒットすると、しばらくして破壊されます。敵が尽きないようにするために、新しい敵を生み出すトリガーを作りました。コードでは、これは関数Addzombiesです。

  1. それらを同じポイントでスポーンしないようにするにはどうすればよいですか。今実行すると、互いに開始し、爆発として跳ね返ります。
  2. どうすれば空中でスタートできるのでしょうか、やってみましたがスポーンしません。

私のコード:

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);
    }
}
4

2 に答える 2

1

どうすればよいかアドバイスしますが、要件に合わせて変更する必要があります

public Transform zombiePrefab; // From the editor drag and drop your prefab


void addZombies()
{   
    // as you need them to be not on the same point
    int randomX = Random.Range(-10.0F, 10.0F);
    for (int i = 0; i < aantal; i++){
        // make a transform
        var zombieTransform = Instantiate(zombiePrefab) as Transform; 
        zombieTransform.position = new Vector3(randomX, 0, 7 + i);
        transform.GetComponent<Rigidbody>().enabled = false;            
        // make it enable when you need and add force to make them fall     
    }
} 
于 2014-12-17T11:20:03.997 に答える
0

渡すゾンビの数と、それらがスポーンできる空間の範囲を表す整数を渡すことをお勧めします。次に、ゾンビをスポーンするためのいくつかの異なる座標を生成するために、ゾンビごとに上記の整数で UnityEngine.Random を使用します。

それらを空中にスポーンさせる場合は、ゾンビをインスタンス化するときに y 座標を増やすだけです。

于 2014-12-16T23:59:11.720 に答える