Hi Everyone!
I’ve been working on a First Person Planet Exploration game for the past month or so and I have run into a problem. I’m trying to randomly place trees onto a mesh of a planet but all I can do is place them at the vertices of the planet. Is there a way to find random points on the planet that aren’t on top of a vertex? Any help would be great! Here is a picture of my planet and the script that I am using.(BTW the planet was modeled in Blender).

var tree:GameObject;
var offset:Vector3;
var verticies:int;
function Start () {
offset=transform.position;
AddTrees();
}
function AddTrees(){
var baseVertices : Vector3[];
var mesh : Mesh = GetComponent(MeshFilter).mesh;
if (baseVertices == null){
baseVertices = mesh.vertices;
}
var vertices = new Vector3[baseVertices.Length];
verticies=vertices.Length;
for(var i=0; i<(vertices.Length);i++){
var vertex = baseVertices*;*

Thanks so much! For anyone who’s curious about this, here is the now the working code and a picture to go with it!

var tree:GameObject;
var pointa:Vector3=Vector3.zero;
var pointb:Vector3=Vector3.zero;
var pointc:Vector3=Vector3.zero;
function Start () {
RandomAdd();
}
function RandomAdd () {
var baseVertices : Vector3[];
var mesh : Mesh = GetComponent(MeshFilter).mesh;
for(var i:int= 0; i < mesh.triangles.Length; i += 3){
var rndA = Random.value;
var rndB = Random.value;
var rndC = Random.value;
pointa = mesh.vertices[mesh.triangles[i + 0]];
pointb = mesh.vertices[mesh.triangles[i + 1]];
pointc = mesh.vertices[mesh.triangles[i + 2]];

First off, be advised that I have not tried this and it is coming right off the top of my head. Now, there is a function called GetTopology listed in the Mesh API page. It claims to get points (as well as other things) from a sub-mesh and use those to render the mesh. My thinking is perhaps you can use such a function and mix it with an array and a random number generator and maybe be left with an array to Instantiate your trees?

Not sure of your requirements. I can think of two approaches. First, instead of generating a random vertex, generate a random triangle from the Mesh.triangles array. The triangle will give you three vertices forming the triangle. You can then generate a random position on the triangle. See Andeeee’s answer here:

The second methods is to generate a position well beyond the planed, and do a Collider.Raycast() back to the planet. Some untested code:

var dir = -Random.onUnitSphere;
var pos = planet.position - dir * someLargerDistance;
var ray = new Ray(pos, dir);
var hit : RaycastHit;
collider.Raycast(ray, hit, someLargerDistance);

‘someLargerDistance’ is some distance guaranteed to be beyond the planet when measured from planet pivot. ‘hit.point’ will be the random position.

HERE you are maybe late but better than never
var tree:GameObject;
var pointa:Vector3=Vector3.zero;
var pointb:Vector3=Vector3.zero;
var pointc:Vector3=Vector3.zero;

function Start () {
RandomAdd();
}

function RandomAdd () {
var baseVertices : Vector3;
var mesh : Mesh = GetComponent(MeshFilter).mesh;
for(var i:int= 0; i < mesh.triangles.Length; i += 3){
var rndA = Random.value;
var rndB = Random.value;
var rndC = Random.value;
var rndTriPoint = (rndA * pointa + rndB * pointb + rndC * pointc) / (rndA + rndB + rndC);