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 Kensharma · Mar 06, 2014 at 01:31 AM · 2dinstantiateplatformgeneration

2D Platform Generation: Spawning Platforms Consecutively

Hey there fellow Unity devs;

I have been playing around with the following script for a few hours now trying to get a platform to spawn once at a fixed location and then spawn another platform at the edge of the previous platform after a delay.

 using UnityEngine;
 using System.Collections;
 
 public class SpawnScript : MonoBehaviour {
 
     public GameObject[] obj;
     public float spawnMin = 1f;
     public float spawnMax = 2f;
 
     // Use this for initialization
     void Start () {
         Spawn();
     }
     
     void Spawn()
     {
         Instantiate (obj [Random.Range (0, obj.GetLength (0))], transform.position, Quaternion.identity);
         Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
     }
 }
 

I have tried a few different methods that I have found around the answers listings but haven't gotten it working yet.

I think the problem has something to do with how I am instructing the game to spawn the next platform. The next platform should spawn at the edge of the previously spawned platform, however I think that the fact that this spawn block is following the camera is messing it up.

How can I compensate for the camera movement or how can I do this without attaching the script to a block following the camera?

Thanks for any help.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fifthknotch · Mar 06, 2014 at 04:18 AM

I don't know exactly how you want your game to look and feel, but I would work it like this:

Load your level with one platform (can be random of course). Attach to your platform the script saying to instantiate the next platform, but do so only if the platform is visible (void OnBecameVisible - builtin unity function). Here's why :

You will start with a scene that has one random platform instantiate. Then it will instantiate the next because it is visible, which will instantiate the next platform, and so on, until a platform leaves the view of your camera. Then as your player moves, that last platform will come into sight and instantiate a platform next it, just out of view. This will result in an infinite amount of random platforms being spawned as your player moves in one direction. If you want any type of delay, yield statements work nicely.

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
avatar image
0

Answer by robertbu · Mar 06, 2014 at 04:42 AM

You have some definition issues here. First you say "at the edge of the previously spawned platform." So is that the left edge, right edge, front, back? Second, when placing so that edges touch, you need to know the size of the item. So for a simple example, lets say that the objects are 1 unit in width, and you want to grow them from left to right. Here is a bit of example code. I've changed your chained Invoke() calls to a coroutine. You should initialize your 'obj' with a prefab of a sphere and a cube (both 1 unit in size). The initial value of 'pos' is where the first one will be placed:

 using UnityEngine;
 using System.Collections;
 
 public class SpawnScript : MonoBehaviour {
     
     public GameObject[] obj;
     public float spawnMin = 1f;
     public float spawnMax = 2f;
     public Vector3 pos = new Vector3(-4,0,0);
     public float size = 1.0f;
     private Vector3 dir = Vector3.right;
 
     void Start () {
         StartCoroutine(Spawn());
     }
 
     IEnumerator Spawn() {
         while (true) {
             Instantiate (obj [Random.Range (0, obj.Length)], pos, Quaternion.identity);
             pos += dir * size;
             yield return new WaitForSeconds(Random.Range (spawnMin, spawnMax));
         }
     }
 }
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
avatar image
0

Answer by Kensharma · Mar 06, 2014 at 09:57 PM

I actually solved this myself by modifying the gameobject this script was attached to so that rather then following the camera, it moves at a fixed interval (each movement is the size of the platform) and then modified the script so that it will spawn after each movement.

Does this work just as well or should I opt for another method? All my platforms are the same length so that isn't a problem.

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 fifthknotch · Mar 07, 2014 at 05:18 AM 0
Share

As long as you don't have it spawning platforms in an infinite loop - all is well as far as I can tell.

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

21 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

Related Questions

Platforms are been generated more and more to the left or right 0 Answers

Trying to Instantiate gameobjects via code... getting plenty of errors 1 Answer

There is a generation lag 1 Answer

I cant get my 2d shooting script to work? 2 Answers

XBox Controller Angle of Fire Incorrect 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