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
0
Question by Dragunas · Feb 08, 2021 at 08:43 PM · spawninggrounded

Spawn object on ground

i made a animal spawn script but my animals are spawning at y 500 so that they dont spawn under the map. how could i make it that my animals spawn on the ground and not 500 on the y axis or under tha map. my map has alot of mountains


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AnimalSpawner : MonoBehaviour
 {
     public Transform animalSpawner;
     public GameObject Animal;
     public int xPos;
     public int zPos;
     public int animalCount = 10;
     private LightningManager lightManager;
     
     void Start()
     {
         lightManager = GameObject.FindObjectOfType<LightningManager>();
     }
 
     void Update()
     {
         if (animalCount <= 50){
             SpawnAnimasl();
         }
     }
 
 
     void SpawnAnimasl()
     {
         while (animalCount < 10)
         {
             xPos = Random.Range(1, 1600);
             zPos = Random.Range(1, 1600);
             Instantiate(Animal, new Vector3(xPos, 500, zPos), Quaternion.identity);
             animalCount += 1;
         }
     }
       
     
 }

Comment
Add comment · Show 1
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 Megaboy238 · Feb 08, 2021 at 08:50 PM 0
Share

The way I would go about it is to find your spawn point at y:500 and raycast down to the map mesh, take note of the distance and take that way from your y:500 and you have your ground spawn point.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by AbandonedCrypt · Feb 09, 2021 at 09:08 AM

Physics.Raycast up from your calculated random xz-position at y = 0 (or wherever your lowest terrain point is) until you hit a ground collider, then instantiate your animal at the hit.point. That should handle both cases of them spawning too far up and below terrain.

(Pseudo)-Code:

 if(Physics.Raycast(rndPos, Vector3.up, out RaycastHit hit))
     Instantiate(prefab, hit.point, Quaternion.identity);

Make sure to check if you hit ground! E.g. by comparing tags, or (ideally) using a layermask.

Comment
Add comment · Show 28 · 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 Dragunas · Feb 09, 2021 at 12:22 PM 0
Share

did you spelled Identity wrong or something, becouse it tells me: 'Quaternion' does not contain a definition for 'Identity'

avatar image Khar03 Dragunas · Feb 09, 2021 at 12:26 PM 1
Share

Try "identity" (small i) instead of "Identity"

avatar image Dragunas Khar03 · Feb 09, 2021 at 12:31 PM 0
Share

identity works but what should i assign as rndPos, a transform or something else? sorry for my bad englisch by the way

Show more comments
avatar image
0

Answer by Khar03 · Feb 09, 2021 at 07:30 AM

You can use raycast to get distance from animal to ground and then move that animal down by that distance.

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 AbandonedCrypt · Feb 09, 2021 at 09:08 AM 0
Share

This does not handle animals spawning below terrain in the case that 500 is lower than e.g. a mountain.

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

114 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

Related Questions

How to randomly spawn objects above the ground? 1 Answer

What is the best way of spawning a customisable gameobject over a network? 0 Answers

Spawning Terrain Around The Player 0 Answers

A warning before a enemy spawns 1 Answer

place a object not in a object 0 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