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
1
Question by Grimbyte · Jul 02, 2015 at 12:49 AM · instantiatemesh

How would I instantiate objects on a mesh?

Hi,

I have a mesh (terrain), and I want to instantiate objects (trees) on it. Unfortunately, my knowledge of C# limits me. :(

I've heard of using raycasts, if this is the solution, how would I go about doing that?

Comment
Add comment
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

2 Replies

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

Answer by zach-r-d · Jul 02, 2015 at 04:58 AM

For a terrain mesh, the easiest way is to pick a random point in the XZ plane of the terrain (accessible through Renderer.bounds) and do a raycast straight downwards starting from just above the highest point in the terrain (also accessible through Renderer.bounds).

For example, to get a random point on the terrain on which a tree could be spawned, it would look something like this:

 Renderer r = GetComponent<Renderer>(); // assumes the terrain is in a mesh renderer on the same GameObject
 float randomX = Random.Range(r.bounds.min.x, r.bounds.max.x);
 float randomZ = Random.Range(r.bounds.min.z, r.bounds.max.z);
 
 RaycastHit hit;
 if (Physics.Raycast(new Vector3(randomX, r.bounds.max.y + 5f, randomZ), -Vector3.up, out hit)) {
     // the raycast hit, the point on the terrain is hit.point, spawn a tree there
 } else {
     // the raycast didn't hit, maybe there's a hole in the terrain?
 }
Comment
Add comment · Show 6 · 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 Grimbyte · Jul 02, 2015 at 03:07 PM 0
Share

Thank you for your answer! I do have a problem though, no errors or warnings are popping up, but the objects don't seem to instantiate. Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class SurfacePopulator : $$anonymous$$onoBehaviour {
 
     public int numberOfObjects;
     public int currentObjects;
     public GameObject objectToPlace;
 
     private float randomX;
     private float randomZ;
     private Renderer r;
 
     void Start() {
 
         r = GetComponent<Renderer>();
         randomX = Random.Range(r.bounds.$$anonymous$$.x, r.bounds.max.x);
         randomZ = Random.Range(r.bounds.$$anonymous$$.z, r.bounds.max.z);
     }
 
     void Update() {
         RaycastHit hit;
         if(currentObjects <= numberOfObjects) {
             if (Physics.Raycast(new Vector3(randomX, r.bounds.max.y + 5f, randomZ), -Vector3.up, out hit)) {
                 Instantiate(objectToPlace, hit.point, Quaternion.identity);
                 currentObjects += 1;
             }
         }
     }
 }

I've traced the problem to this line:

if (Physics.Raycast(new Vector3(randomX, r.bounds.max.y + 5f, randomZ), -Vector3.up, out hit)) { The first if statement is passed, but the second one isn't.

avatar image TheChosenOnee Grimbyte · Dec 15, 2016 at 12:00 PM 0
Share

Hey! I saw your post and decided to ask you about the code you've used. I am a complete newbie to Unity. $$anonymous$$y problem is that I have series of meshes representing a map (2D), and I need to create instances of Image Gameobject inside the mesh with randomised locations over each of the mesh layers. The problem is that when I tried to use the code which you have posted it did not show anything, could be some advice on this matter? I would really appreciate your support!

avatar image zach-r-d · Jul 02, 2015 at 07:59 PM 1
Share

Ah, thanks for tracing the problem. First, move the assignments to randomX and randomZ between the two if statements; otherwise the ray will fire from the same point every time and the trees will form a single column. Second, make sure the terrain has a collider on it; raycasts can only hit things with colliders.

avatar image Grimbyte · Jul 03, 2015 at 03:19 AM 1
Share

It works! :) Thanks for your help.

avatar image eduardoks1998 · May 11, 2020 at 08:24 AM 0
Share

Work's fine too me thanks!!!

avatar image Silent_Whisper · Feb 20 at 07:11 PM 0
Share

yo wth thanks dude i've been looking for a lot of time for something similar not knowing how to phrase it. you saved me a lot of time! thanks!

avatar image
0

Answer by Sawula · Feb 15, 2017 at 03:14 PM

A bit old I know, but this is very close to what I'm doing, except sometime when I instantiate my object, they spawn halfway into the mesh instead of on its surface. Is there any way to avoid this?

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

27 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

Related Questions

How to create a new GameObject and assign a Mesh that is just being created in a same function to it? 0 Answers

When generating a procedural voxel mesh, i get something weird. 1 Answer

How do I instantiate objects above 'sealevel'? 1 Answer

Mirroring a mesh? 1 Answer

instantiate object and align with object surface 2 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