Randomly generated moving objects that slowly approach
this code is suppose to generate randomly placed planes in the distance that will fly towards you while the objective is to dodge them, however it just spawns random planes that don't move. It just gradually spawns them closer to your plane and keeps spawning them even if it goes past it, while the ones previously spawned stay in the same spot, so it's just a trail of non-moving planes, how would I code it so they approach me? Anything helps, thanks!
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, 8);
GameObject cloudInt = Instantiate (cloud) as GameObject;
for (int i = 0; i < secondRand; i++)
{
x = Random.Range (-8, 16);
y = 0;
z = 287;
intPos = new Vector3 (x, y, z);
distance -= 1;
cloudInt.transform.position = intPos;
}
}
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;
}
}
}
}
Your answer
Follow this Question
Related Questions
Drawing a GUI line 0 Answers
How to create a script to control a path like in game 'Tbe Witnsss' 0 Answers
Corrupt file 1 Answer
The "scene" button disappeared?,The "scene" button disappeared. 0 Answers