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 twoface262 · Apr 15, 2013 at 01:14 AM · androidjavascript

Spawning random terrain pieces

Hello! I'm trying to set up a random terrain generator. I'm using e2d for the terrain, and what I did was create about 10 pieces using that. I put them all in an array, and I'm using this code to spawn pieces at random from the array.

   var spawned = 0;
     var start : Transform;
     var end : Transform;
     var player : GameObject;
     var ptimer = 0;
     var terrain : Transform[];
     var ttype = 0;
     function Start () {
     ttype = Random.Range(1, terrain.Length);
     }
     
     function Update () {
     if(player == null){
     ptimer ++;
     if(ptimer >= 10){
     player = GameObject.FindGameObjectWithTag("Player");
     }
     }
     
     if(player){
     var pdis1 = Vector3.Distance(player.transform.position, end.position);
     var pdis2 = Vector3.Distance(player.transform.position, start.position);
     if(pdis1 <= 4000){
     if(spawned == 0){
     spawnend();
     spawned = 1;
     
     }
     }
     
     }
     }
     
     
     function spawnend(){
     //yield WaitForSeconds(5.0); 
     var terrainss = Instantiate(terrain[ttype], Vector3(transform.position.x + terrain[ttype].GetChild(0).collider.bounds.size.x, transform.position.y,7), Quaternion.Euler(0,0,0));
     var ts : randomhillspawn = terrainss.GetComponent("randomhillspawn");
     ts.spawned = 0;
     }


The problem I'm facing is that it'll spawn 5 - 6 different pieces in one location when it shouldn't. What am I doing wrong here?

Comment
Add comment · Show 2
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 cdrandin · Apr 15, 2013 at 01:34 AM 0
Share

Well they are being placed on top of each other because of Vector3(transform.position.x + terrain[ttype].GetChild(0).collider.bounds.size.x, ... sure collider.bounds.size is not the same every time, but they are being instantiated in relatively the same spot.

avatar image twoface262 · Apr 15, 2013 at 02:32 AM 0
Share

How do I fix this then?

1 Reply

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

Answer by Glister · Apr 15, 2013 at 06:24 AM

Try placing spawned = 1; before the call to the function spawnend. It also looks as though you are resetting spawned to 0 at the end of spawnend and have the call for spawnend in the update function. The update function is called every frame so upwards to 60 times in a second. This can have adverse affects. You should check the time between the last call to spawnend.

declare a variable to store the time value.

 var timeLastSpawnEnd = Time.time;

Then change the following code.

     if(player){
        var pdis1 = Vector3.Distance(player.transform.position, end.position);
        var pdis2 = Vector3.Distance(player.transform.position, start.position);
        if(pdis1 <= 4000){
            if(spawned == 0 && Time.time > timeLastSpawnEnd +1 ){
                spawnend();
                spawned = 1;
          
             }
        }
          
      }
 }
      
      
 function spawnend(){
     //yield WaitForSeconds(5.0); 
     var terrainss = Instantiate(terrain[ttype], Vector3(transform.position.x + terrain[ttype].GetChild(0).collider.bounds.size.x, transform.position.y,7), Quaternion.Euler(0,0,0));
     timeLastSpawnEnd = Time.time;
     var ts : randomhillspawn = terrainss.GetComponent("randomhillspawn");
     ts.spawned = 0;

 }


This should allow you to make a call to spawnend only once every second.

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

12 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

Related Questions

About ListItems in Unity3D 1 Answer

Load function not working propperly? 1 Answer

Loading from multiple files? 2 Answers

Object deformation on activat 0 Answers

Saving items to a text file 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