Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
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
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
3

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
Add comment · 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
0

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
Add comment · 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.

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

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

Random Terrain Generation 2 Answers

How to access property of a prefab before Instantiating. 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