Spawn Prefab on Terrain
Oh Rally » Devlog
This script is useful to spawn objects from a list onto Terrain.
I didn't work out how to get random x-rotation but maybe you will find it useful nonetheless.
Thanks to the Unity community for help along the way.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SpawnTogether : MonoBehaviour { public GameObject[] myObjects; public int cactusNumber; void Start() { CreateCacti(cactusNumber); } void CreateCacti(int cactiNum) { for (int i = 0; i < cactiNum; i++) { //generate int from the array int randomIndex = Random.Range(0, myObjects.Length); Vector3 randomSpawnPosition = new Vector3(Random.Range(-300, 300), i, Random.Range(-300, 300)); //pass random spawn position to terrain height at chosen spot float spotHeight = Terrain.activeTerrain.SampleHeight(randomSpawnPosition); //create final position and feed in the values we generated Vector3 pos = transform.position; pos.y = spotHeight; pos.x = randomSpawnPosition.x; pos.z = randomSpawnPosition.z; Instantiate(myObjects[randomIndex], pos, Quaternion.identity); } } }
Get Oh Rally
Download NowName your own price
Oh Rally
A casual rally game.
More posts
- Grass shaderMar 03, 2023
- Original Soundtrack Mp3Mar 02, 2023
- Car Modelling ImagesMar 02, 2023
Leave a comment
Log in with itch.io to leave a comment.