Random Obstacle spawn (Infinite Runner)
Hey Guys.
I'm working on this infinite runner type of game. I've made a tile manager that randomly spawns a set of PreFab-tiles in front of the player while removing the ones falling behind.
Now I want to add some obstacles. However, I'm not interested in placing all the obstacles manually. I would like to spawn them randomly on and above the tiles in front of the player.
I'm not sure if you get what I mean, but please. Take a look at these pictures: http://imgur.com/a/eBVct
So far I've made a script that spawns a given GameObject inside an area. But while the area seems to move with the new tiles see the pictures the obstacles is still being spawned at the initial spawnarea...
Here is my code, hope you guys understand and can help me out:
using UnityEngine;
public class SpawnObject : MonoBehaviour {
public GameObject killObstaclePreFab;
public Vector3 center;
public Vector3 size;
// Use this for initialization
void Start () {
SpawnObstacles();
}
public void SpawnObstacles()
{
Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), Random.Range(-size.y / 2, size.y / 2), Random.Range(-size.z / 2, size.z / 2));
Instantiate(killObstaclePreFab, pos, Quaternion.identity);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1, 0, 0, 0.5f);
Gizmos.DrawCube(transform.position + center, size);
}
}
Kind regards Benjamin :)
Answer by Blaze283 · Jul 02, 2017 at 05:37 PM
@BenjaminStarostka You're only spawning them? no movement for the obstacles?
Your answer
Follow this Question
Related Questions
problems with instantiating a moving prefab. 0 Answers
Instantiating Objects randomly on a sphere with a set distance radius 0 Answers
Problem of procedural generation using randomness 2 Answers
EventTrigger on button not recognizing instantsiated player 1 Answer
Collisions only working in the editor but not in the build 0 Answers