Unity 5随机生成的移动物体慢慢靠近

时间:2017-05-14 05:55:32

标签: c# unity5

这个代码假设生成随机放置的云和飞机,它们将飞向你,同时目标是躲避它们,但是它只会产生不会移动的随机飞机。它只是逐渐将它们催生在离你的飞机更近的地方并且即使经过它也会继续产生它们,而之前产生的那些停留在同一个地方,所以它只是一条非移动的飞机,我将如何编码它接近我了吗?什么都有帮助,谢谢!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CloudGen : MonoBehaviour {

    public GameObject cloud;
    public GameObject enemy;

    int firstRand;
    int secondRand;
    int distance = 12;
    int time = 0;


    float x;
    float y;
    float z;

    Vector3 intPos;


    void Update () {
        if (Input.GetButtonDown ("left") || Input.GetButtonDown ("right")) 
        {
            firstRand = Random.Range (1, 3);
            if(firstRand == 1)
            {
                secondRand = Random.Range (1, 3);
                GameObject cloudInt = Instantiate (cloud) as GameObject;
                for (int i = 0; i < secondRand; i++) 
                {
                    intPos = new Vector3 (Random.RandomRange(1, 4), 0, distance);
                    cloudInt.transform.position = intPos;
                    cloudInt.transform.Translate (distance, 0, 0);
                    distance -= 1;
                }
            }
            if (firstRand == 2) 
            {
                secondRand = Random.Range (1, 8);

                for (int i = 0; i < secondRand; i++) 
                {
                    intPos = new Vector3 (Random.RandomRange(1, 4), 0, distance);                   }
                    GameObject enemyInt = Instantiate (enemy) as GameObject;
                    enemyInt.transform.position = intPos;
                    enemyInt.transform.Translate (distance, 0, 0);
                    distance -= 1;
                }
            }
        }
    }

0 个答案:

没有答案