• 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
Question by Max 4 · Jun 11, 2011 at 04:48 AM · instantiateterrainrandomautomatic

Spawning above the terrain

For my multiplayer game, I want random spawning. This is what I'm using:

 Network.Instantiate (Player, Vector3 (Random.Range (5, 95), transform.position.y+4, Random.Range (5, 95)), transform.rotation);

It doesn't work, but it isn't what I want anyway. How could I make it so that it always spawns a bit above the ground (on the y axis)?

Comment
Extrakun
Nicolaj Schweitz

People who like this

2 Show 0
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

  • Sort: 
avatar image

Answer by Nicolaj Schweitz · Nov 28, 2013 at 09:52 PM

Hey! I have a script like this, however not taking the object's pivot into account as Ashkan_gc

  1. Instantiate a prefab with the following script as a component.

  2. Put the ground in a layer called ground.

  3. Set the mask on the script to the ground layer.

      using UnityEngine;
         using System.Collections;
     
         public class PutObjectOnGround : MonoBehaviour {
         
         public LayerMask mask = -1;
         float radius;
         
         void Start () {
             // set the vertical offset to the object's collider bounds' extends
             if (GetComponent<Collider>() != null) {
                 radius = GetComponent<Collider> ().bounds.extents.y;
             } else {
                 radius = 1f;
             }
             
             // raycast to find the y-position of the masked collider at the transforms x/z
             RaycastHit hit;
             // note that the ray starts at 100 units
             Ray ray = new Ray (transform.position + Vector3.up * 100, Vector3.down);
             
             if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask)) {        
                 if (hit.collider != null) {
                     // this is where the gameobject is actually put on the ground
                     transform.position = new Vector3 (transform.position.x, hit.point.y + radius, transform.position.z);
                 }
             }
         }
     }
     
    
Comment
tbonetimmons
theo-pnv
Toadill9114

People who like this

3 Show 1 · 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 Toadill9114 · Sep 14, 2018 at 08:34 PM 0
Share

@Nicolaj Schweitz I was looking for this thank you!

avatar image

Answer by Ashkan_gc · Jun 11, 2011 at 05:01 AM

first do a raycast toward ground and find it's position and then spawn a litle bit above it. it depends on the pivot of your object too. you should calculate the distance between pivote and bottom of your object and then position it based on that. procedural object placement is a really fun topic.

Comment

People who like this

0 Show 1 · 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 Max 4 · Jun 11, 2011 at 05:04 AM 0
Share

Hmm, the object isn't in any particular place. The spawner is just an empty GameObject.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Procedural Terrain? 3 Answers

Need help with randomizing starting location of terrains 0 Answers

Random Terrain in 2d Game? 0 Answers

Instantiating multiple objects, Destroying and instantiating on trigger + delay 0 Answers

Moving instantiated prefab from randomly chosen startPoint to randomly chosen endPoint 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