- Home /
Spawning pre-selected objects randomly at fixed locations (2D game, #C)
Hello fellow programmers,
I am trying to create a game which works with a BackGround Looper (script that creates an endless game with minimum amount of background panels).
As can be seen in the screenshot below the game is vertically and the player is floating upwards trying to dodge the spikes.
My question is therefore: With which script or method can I spawn these pre-selected spikes randomly at fixed locations, for example, as can be seen above when the back ground looper is looping them.
I have seen tutorials where the creater had an inspector with selection bars where he had to drag each spike texture in. The script was choosing between all the spikes available and placed them randomly at pre selected locations (in my game it has to be on the pole).
The back ground looper script I am using can be seen below.
using UnityEngine;
using System.Collections;
public class BGLooper : MonoBehaviour {
int numBGPanels = 4;
void OnTriggerEnter2D(Collider2D collider) {
Debug.Log ("Triggered: " + collider.name);
float widthofBGObject = ((BoxCollider2D)collider).size.y;
Vector3 pos = collider.transform.position;
pos.y += widthofBGObject * numBGPanels;
collider.transform.position = pos;
}
}
Thank you in advance!
With kind regards,
Steve