Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by barrenhook · Sep 01, 2014 at 08:03 AM · instantiateplanet

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

unity tree problem.png (404.0 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lluvensanger · Nov 18, 2016 at 05:07 PM 0
Share

Were you the person who then in the future launched No $$anonymous$$an's Sky?

avatar image Endico lluvensanger · Dec 02, 2016 at 08:58 AM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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


unity tree solved.png (403.1 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShinyTaco · Nov 21, 2014 at 04:42 AM 0
Share

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!

avatar image vozer · Aug 09, 2015 at 03:42 PM 0
Share

also unsure about this part, would love an answer to that

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
  }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Split a cube into several pieces? 1 Answer

scripting question 3 Answers

Trying to assign GUIText from heirarchy into instantiated prefab 1 Answer

Prefab GameObject's can not be made active! 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges