- Home /
Random Instantiate onto Planet
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[i];
Instantiate (tree, vertex, Quaternion.identity);
}
}
[1]: /storage/temp/31830-unity++tree+problem.png
Were you the person who then in the future launched No $$anonymous$$an's Sky?
Nah but i am working on something in relation if you want my planetary source code its up for grabs for the first 5 people who send a request to my email(of course you have your copy guaranteed @lluvensanger ) why ? ...i want indie space exploration games to soar .. even at the cost of my source so yes only 5 people can claim this source, abiding you do not sell or send this to any other person (please?). Email : jordankmuguluma@gmail.com.
Answer by robertbu · Sep 01, 2014 at 08:43 AM
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:
http://forum.unity3d.com/threads/random-instantiate-on-surface-of-mesh.11153/
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.
Answer by barrenhook · Sep 01, 2014 at 05:53 PM
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]];
var rndTriPoint = (rndA pointa + rndB pointb + rndC * pointc) / (rndA + rndB + rndC);
Instantiate (tree, rndTriPoint, Quaternion.identity);
}
}
[1]: /storage/temp/31864-unity+tree+solved.png
Hey, great code, thanks for sharing.
Can you clarify this part:
var rndTriPoint = (rndA pointa + rndB pointb + rndC * pointc) / (rndA + rndB + rndC);
It throws an error.
I change the code, but the trees don't spawn on the mesh.
Thanks!
Answer by superpentil · Sep 01, 2014 at 08:42 AM
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?
Here is the MeshTopology Page: http://docs.unity3d.com/ScriptReference/MeshTopology.html
I've not tried this before, so hopefully it's a good place to start from.
Answer by Endico · Sep 12, 2016 at 04:09 PM
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);
pointa = mesh.vertices[mesh.triangles[i + 0]];
pointb = mesh.vertices[mesh.triangles[i + 1]];
pointc = mesh.vertices[mesh.triangles[i + 2]];
Instantiate (tree, rndTriPoint, Quaternion.identity);
}
}