Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by Kirbsssys · Oct 22, 2021 at 02:24 AM · instantiateprocedural generation

Randomly spawn an object on a procedurally generated plane

I wanna know how to spawn different objects on my procedurally generated map, like trees or rocks. Here's to code I'm using.

 using ProceduralToolkit.Samples.UI;
 using UnityEngine;

 namespace ProceduralToolkit.Samples
 {
     public class LowPolyTerrainExample : ConfiguratorBase
     {
     public MeshFilter terrainMeshFilter;
     public MeshCollider terrainMeshCollider;
     public RectTransform leftPanel;
     public bool constantSeed = false;
     public LowPolyTerrainGenerator.Config config = new LowPolyTerrainGenerator.Config();

     private const int minYSize = 1;
     private const int maxYSize = 10;
     private const float minCellSize = 0.3f;
     private const float maxCellSize = 1;
     private const int minNoiseFrequency = 1;
     private const int maxNoiseFrequency = 8;

     private Mesh terrainMesh;

     private void Awake()
     {
         Generate();
         SetupSkyboxAndPalette();

         InstantiateControl<SliderControl>(leftPanel)
             .Initialize("Terrain height", minYSize, maxYSize, (int) config.terrainSize.y, value =>
             {
                 config.terrainSize.y = value;
                 Generate();
             });

         InstantiateControl<SliderControl>(leftPanel)
             .Initialize("Cell size", minCellSize, maxCellSize, config.cellSize, value =>
             {
                 config.cellSize = value;
                 Generate();
             });

         InstantiateControl<SliderControl>(leftPanel)
             .Initialize("Noise frequency", minNoiseFrequency, maxNoiseFrequency, (int) config.noiseFrequency, value =>
             {
                 config.noiseFrequency = value;
                 Generate();
             });

         InstantiateControl<ButtonControl>(leftPanel).Initialize("Generate", () => Generate());
     }

     private void Update()
     {
         UpdateSkybox();
     }

     public void Generate(bool randomizeConfig = true)
     {
         if (constantSeed)
         {
             Random.InitState(0);
         }

         if (randomizeConfig)
         {
             GeneratePalette();

             config.gradient = ColorE.Gradient(from: GetMainColorHSV(), to: GetSecondaryColorHSV());
         }

         var draft = LowPolyTerrainGenerator.TerrainDraft(config);
         draft.Move(Vector3.left*config.terrainSize.x/2 + Vector3.back*config.terrainSize.z/2);
         AssignDraftToMeshFilter(draft, terrainMeshFilter, ref terrainMesh);
         terrainMeshCollider.sharedMesh = terrainMesh;
      }

    }
 }
Comment
Add comment · Show 6
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 Captain_Pineapple · Oct 22, 2021 at 06:22 AM 0
Share

can you please add more context? for a question like this please write at least 5 sentences about where you want to place what, what you have tried and why it does not work.

avatar image logicandchaos · Oct 22, 2021 at 02:23 PM 0
Share

just get the bounds of your plane and generate a random coordinate that is within those bounds. Then instantiate your enemy and set position to the coordinates you generated.

avatar image Kirbsssys · Oct 22, 2021 at 03:02 PM 0
Share

That would work if I want it to fall from space, I want the objects to be spawned on top of the ground with different positions, so I can have a tree without a rigid body be spawned on the ground proceduraly, and I don't know how to achieve this

avatar image Captain_Pineapple Kirbsssys · Oct 22, 2021 at 10:00 PM 0
Share

well one cheap but reliable way would be to raycast on your terrain from a point where you can be sure that it is above your maximum possible terrain height. Then place the object on the hitpoint.

avatar image Kirbsssys Captain_Pineapple · Oct 22, 2021 at 11:53 PM 0
Share

How would you do that, I have no clue how to use raycasts.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

158 People are following this question.

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

Related Questions

I wanna know how to randomize perlin noise 2 Answers

Scene generation question 0 Answers

Overlapping instantiation issues. Help? 1 Answer

Checking if object intersects? 1 Answer

Instantiate prefabs next to each other? 1 Answer


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