I'm very new at coding (like two weeks), so forgive me for the silly question. But I'm trying to code a block sliding 3d game in unity, where the obstacles randomly generate, forever, the random generation is fine, but for my life, i cannot figure out how to get the obstacles to spawn at random Z positions. i"m currently stuck with a CS0117 error, so i cant test my latest attempt to fix this.
here's my current code:
using UnityEngine;
public class BlockSpawner : MonoBehaviour
{
public Transform[] SpawnPoints;
public GameObject blockPrefab;
public float timeBetweenWaves = 1f;
private float timeToSpawn = 2f;
public class Random { }
void Update()
{
if (Time.time >= timeToSpawn)
{
SpawnBlocks();
timeToSpawn = Time.time timeBetweenWaves;
}
}
void SpawnBlocks()
{
//Spawning blocks in
int randomIndex = Random.Range(0, SpawnPoints.Length);
{
for (int i = 0; i < SpawnPoints.Length; i )
if (randomIndex != i)
{
// generate random position
var viewRange = this.Size - SpawnPoints.Size;
var left = random.Next(0, viewRange.Width);
var top = random.Next(0, viewRange.Height);
// set the random position
SpawnPoints.Location = new Point(left, top);
Random random = new Random();
}
}
}
}
And Heres is my last working Code (note this has no random location code attempts):
using UnityEngine;
public class BlockSpawner : MonoBehaviour
{
public Transform[] SpawnPoints;
public GameObject blockPrefab;
public float timeBetweenWaves = 1f;
private float timeToSpawn = 2f;
void Update()
{
if (Time.time >= timeToSpawn)
{
SpawnBlocks();
timeToSpawn = Time.time timeBetweenWaves;
}
}
void SpawnBlocks()
{
//Spawning blocks in
int randomIndex = Random.Range(0, SpawnPoints.Length);
{
for (int i = 0; i < SpawnPoints.Length; i )
if (randomIndex != i)
{
Instantiate(blockPrefab, SpawnPoints[i].position, Quaternion.identity);
}
}
}
}
Please help!
CodePudding user response:
I will guess at the error and say this is wrong
var viewRange = this.Size - SpawnPoints.Size;
var left = random.Next(0, viewRange.Width);
var top = random.Next(0, viewRange.Height);
// set the random position
SpawnPoints.Location = new Point(left, top);
Random random = new Random();
you are calling methods on 'random' before you have set it up. YOu need
var viewRange = this.Size - SpawnPoints.Size;
Random random = new Random();
var left = random.Next(0, viewRange.Width);
var top = random.Next(0, viewRange.Height);
// set the random position
SpawnPoints.Location = new Point(left, top);
But I will point out 2 things
- unity has given you its random number generator, and you already used it (Random.Range) but for some reason you decide to use the c# standard one
- creating a new Random each time round that loop will keep generating the same numbers over and over again if called in rapid succession better is to create the Random once at the start of the function (or use the Unity one)
CodePudding user response:
To Generate Obstacles At Random Z position. Am I keeping it much Simple!.
To Do this we have to keep these things in mind:
1: The X and Y Coordinates are not going to change.
2: The only Z coordinates are going to change.
Solution: Generate a new Vector3 that will contains same X and Y coordinates but random Z coordinates.
Code:
// the game object (mean obstacle to generate)
public GameObject ObstaclePrefab;
// this will be the coordinates that will not going to change (X and Y coordinates as they will be same for all obstacles but only the Z coordinates will change.)
public Vector2 ConstantCords = new Vector(0, 0);
// the max and minimum range for obstacles to be generated (X represents the negative [minimum] while y will represent the positive [maximum] position (Cordinates) )
public Vector2 MaxZCords = new Vector2(-50f, 50f);
// this method will generate the obstacle at random position
public void GenerateRandomObstacle()
{
// getting a random Z coordinates
// Random.Range will give random values
float Z = Random.Range(MaxZCords.x, MaxZCords.Y);
// getting position to generate the obstacle
// putting the constant and the random Z value
Vector3 pos = new Vector3(ConstantCords.x, ConstantCords.y, Z);
// generating the obstacle at the generated random position
// we are setting the rotation to rotation of the prefab
Instantiate(ObstaclePerfab, pos, ObstaclePrefab.transfrom.rotation);
}