Hello. How to make random items on map?
Hey. I am creating location-based game like Pokemon GO. And as I said in topic I want to make random items spawning on the map (but SCPs will be here instead of pokemons). Thanks for any kind of help.
Answer by diegonv · Sep 06, 2020 at 08:00 PM
make an array of Gameobjects (public Gameobjets[] ga) then add every prefab of your pokemons there.
then use something like this
for(int i=0; i < amount_pok; i++) //replace amount by the amount to spawn
{
Gameobject one_pok = Instantiate( ga[Random.Range(0, ga.Lenght-1)] );
one_pok.transform.position = new Vector3( Random.Range(0, 100f), Random.Range(0, 100f), Random.Range(0, 100f) );
}
It will generate a cube of 100x100x100 where randomly will appear. To put it in ground, use some raycast going from a high altitude to down, and when it collides into ground, apply the Y coordinate of that collision in the one_pok position Y (inside the for). Or you can just use some rigid body to make fall them into ground by gravity (after spawing in air, the spawing cube must be higher than terrain so they dont spawn under it)
I just rewrite a your answer a bit )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjeectGenerator : $$anonymous$$onoBehaviour
{
GameObject[] prefabsArray = new GameObject[amountOfPrefabs]; //You can just dynamicaly change this value
void Start()
{
for(int curObjects=0; curObjects < maxObjects; curObjects++)
{
Vector3 spawnPos = new Vector3(Random.Range(0, 100f), Random.Range(0, 100f), Random.Range(0, 100f));
GameObject choosedObject = prefabsArray[Random.Range(0,
prefabsArray.Lenght-1)];
Gameobject spawnedObject = Instantiate(choosedObject, spawnPos, Random.rotation);
//You can make some manipulations with spawned object in future
}
}
}
Oh, thanks. But how i can implement this script in GOmap?