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
0
Question by Chyakka · Mar 13, 2017 at 08:10 PM · terrainvector3

Enemy Pooling on a Terrain

I use an object pooling system

     height = Terrain.activeTerrain.SampleHeight (enemy.transform.position)
         + Terrain.activeTerrain.transform.position.y;

     Vector3 spawnPoint = new Vector3();

     spawnPoint.x = Random.Range (2000, 600);
     spawnPoint.y = (height.y);
     spawnPoint.z = Random.Range (1500, 3500);

This is what I try to use^

But because I use NavMesh it needs to be instantiated ON the terrain. My solution above does not work.

The question I pose is: How would I script a bumpy terrains vector 3 height like I did above except work?

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

1 Reply

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

Answer by Puck · Mar 13, 2017 at 09:37 PM

I'm pretty sure you have the right idea, you just need to subtract the terrain's position. You want your enemy's position to be relative to your terrain, so you nullify the terrain's position by subtracting it so that if your terrain were at [-10, 0, -10] the result would be to add [10, 0, 10] to the enemy position.

Try this:

 // TerrainSpawner.cs
 using UnityEngine;
 
 public class TerrainSpawner : MonoBehaviour {
     public void Spawn(Terrain terrain, GameObject template)
     {
         Bounds box = terrain.terrainData.bounds;
 
         // Get random position in terrain space
         Vector3 pos = new Vector3(Random.Range(box.min.x, box.max.x), 0, Random.Range(box.min.z, box.max.z));
 
         // Transform to world space
         pos += terrain.transform.position;
 
         // Sample height
         pos.y = terrain.SampleHeight(pos);
 
         Instantiate(template, pos, Quaternion.identity);
     }
 }

Sample Usage:

 public GameObject obj;
 
 private void Start()
 {
     for (var i = 0; i < 1000; i++)
         GetComponent<TerrainSpawner>().Spawn(Terrain.activeTerrain, obj);
 }


Comment
Add comment · Show 3 · 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 Chyakka · Mar 14, 2017 at 05:28 PM 0
Share

Sadly, not. (also I changed the first word: height to height.y to fix an error) but despite the terrain's Y being 0 the Enemy's always spawn at 337 unless it luckily lands on the terrain which in that case it will not spit out errors and will resume it's navmesh duties as usual. But the chances of the enemies actually landing on the terrain and not floating in the sky spitting a stupid amount of errors just collapsing the game down to staggeringly bad framerates. Thanks for trying though.

avatar image Puck Chyakka · Mar 15, 2017 at 02:59 AM 0
Share

It sounds like your problem is no longer finding the height of the terrain, and more finding the bounds. I updated my answer with a complete solution for you so give that a try and if it's helpful don't forget to accept the answer. Good luck with your project!

avatar image Chyakka · Mar 15, 2017 at 01:23 PM 0
Share

By golly, it works. There is a weird issue which is making the enemies spawn in a herd despite the terrain being massive but that I can fix, Thanks for fixing this problem which is not touched upon in Unity Answers for whatever reason.

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

CLick on terrain and record vector3 location I clicked at? 3 Answers

Make a simple tree 1 Answer

Change texture on terrain based on Vector3 at runtime 0 Answers

Make mesh triangles have a single normal vector to make them stay triangles?? 2 Answers

Terrain and Rigidbody2D incompatible for XZ Topdown?,Using RigidBody2D and Terrain incompatible in top-down game? 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