Hello,
I have a functioning script that randomly generates a set number of rocks in my game. I tried to add to the script that would make all the rocks spawn at the terrain height to stop the from spawning under the terrain. My problem is it isn’t working, some of the rocks spawn in mid air, a little bit above the terrain and I can’t seem to figure out why.
This is the script attached to my Terrain object:
using UnityEngine;
using System.Collections;
public class RockSpawn : MonoBehaviour {
public int rocksToSpawn = 600;
public GameObject rock;
public
// Use this for initialization
void Start () {
for (int i=0; i < rocksToSpawn; i++){
float height = Terrain.activeTerrain.SampleHeight(rock.transform.position);
Vector3 randSpawn = new Vector3(Random.Range(200, 2000), height, Random.Range(200, 2000));
Instantiate(rock, randSpawn, Quaternion.AngleAxis(Random.Range(-180.0f, 180.0f), transform.right));
rock.transform.position = new Vector3(rock.transform.position.x, height, rock.transform.position.z);
}
}
// Update is called once per frame
void Update () {
}
}
Any idea why my rocks seem to spawn in mid air?