I am attempting to make a flappybird style game that is underwater with the PC dodging mines and collecting fish to score points. The issue that I am having is that the mines spawn off screen as intended and fly across the screen, however, they spawn in a straight line. I am following a tutorial as I dont know C# so I am practicing to get knowledge up. But in doing so I am not sure where I am going wrong and google searched yielded no solution.
This is the code for spawning in the mines
private void HandleMineSpawning() {
Timer -= Time.deltaTime;
if (Timer < 0) {
Timer = TimerMax;
float heightEdge = 10f;
float minHeight = offset heightEdge;
float totalHeight = camSize * 2;
float maxHeight = totalHeight - offset *.5f - heightEdge;
float height = Random.Range(minHeight, maxHeight);
CreateMineOffset(height, offset, mineSpawn);
}
And the code that should create an offset on the Y-axis
private void Awake() {
minelist = new List<Mine>();
TimerMax = 1.5f;
offset = 20;
}
private void CreateMineOffset(float offsetY, float offsetSize, float xPosition) {
CreateMine(offsetY - offsetSize * .5f, xPosition);
CreateMine(camSize * 2f - offsetY - offsetSize * .5f, xPosition);
}
CodePudding user response:
This was written for a 3d game but I am sure you can modify it for a platformer game. Set the y spread to a high value and set the x to a low value. The Z spread can stay at zero for your game.
Hope this helps.
public GameObject itemsToSpread;
GameObject spawn;
public int numberOfItemsToSpawn;
public float Space = 10;//Distance between game objects
//Offset values go here
public float itemXSpread = 5;
public float itemYSpread = 100;
public float itemZSpread = 0;//Add value here for a 3d distribution
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < numberOfItemsToSpawn; i )
{
SpreadItem();
}
}
void SpreadItem()
{
Vector3 ranPos = new Vector3(Random.Range(-itemXSpread, itemXSpread) Space, Random.Range(-itemYSpread, itemYSpread) Space, Random.Range(-itemZSpread, itemZSpread) Space) transform.position;
spawn = Instantiate(itemsToSpread, ranPos, Quaternion.identity);
}
Output:
For your game, try these values.