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 points · Jan 11, 2014 at 11:43 AM · 2djumpprefabsendless runner

How Do I Create Random Platforms in a 2d Game?

I was trying to figure on how to create them but nothing seems to be working. I have a Platform prefab and I want this to be randomly created.

This is the code I was trying. But this is not working. nothing happens.

I appreciate any kind of help.

 using UnityEngine;
 using System.Collections.Generic;
 
 public class PlatformManager : MonoBehaviour {
     
     public Transform prefab;
     public int numberOfObjects;
     public float recycleOffset;
     public Vector3 startPosition;
     public Vector3 minSize, maxSize, minGap, maxGap;
     public float minY, maxY;
     
     private Vector3 nextPosition;
     private Queue<Transform> objectQueue;
     
     void Start () {
         objectQueue = new Queue<Transform>(numberOfObjects);
         for(int i = 0; i < numberOfObjects; i++){
             objectQueue.Enqueue((Transform)Instantiate(prefab));
         }
         nextPosition = startPosition;
         for(int i = 0; i < numberOfObjects; i++){
             Recycle();
         }
     }
     
     void Update () {
         if(objectQueue.Peek().localPosition.x + recycleOffset < PlayerScript.distanceTraveled){
             Recycle();
         }
     }
     
     private void Recycle () {
         Vector3 scale = new Vector3(
             Random.Range(minSize.x, maxSize.x),
             Random.Range(minSize.y, maxSize.y),
             Random.Range(minSize.z, maxSize.z));
         
         Vector3 position = nextPosition;
         position.x += scale.x * 0.5f;
         position.y += scale.y * 0.5f;
         
         Transform o = objectQueue.Dequeue();
         o.localScale = scale;
         o.localPosition = position;
         objectQueue.Enqueue(o);
         
         nextPosition += new Vector3(
             Random.Range(minGap.x, maxGap.x) + scale.x,
             Random.Range(minGap.y, maxGap.y),
             Random.Range(minGap.z, maxGap.z));
         
         if(nextPosition.y < minY){
             nextPosition.y = minY + maxGap.y;
         }
         else if(nextPosition.y > maxY){
             nextPosition.y = maxY - maxGap.y;
         }
     }
 }
 

the problem I face here is that the platform move ahead of the camera...

Comment
Add comment · Show 4
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 points · Jan 11, 2014 at 07:19 PM 0
Share

I did. please help me out with this

Thank you

avatar image helloiam · Jan 11, 2014 at 07:26 PM 0
Share

On which axis do you want it to be random?

avatar image helloiam · Jan 11, 2014 at 07:30 PM 0
Share

Otherwise just change it to: position.x += (scale.x 0.5) Random.Range(1, scale.x); and do this also for the y axis except you will need to change the .x to .y. Rule 40 and 41 or of that doesn't work try these at rule 50 etc.

avatar image agies1 · Jan 11, 2014 at 07:33 PM 0
Share

Possible Issuer 1

if(objectQueue.Peek().localPosition.x + recycleOffset < PlayerScript.distanceTraveled)

Your objects are recycled as the Player $$anonymous$$oves a certain distance, which either means the distance is incorrect or the player is outpacing the camera. Can you provide your PlayerScript?

Possible Issue 2

You aren't providing enough platforms, and your platforms are being recycled too early.

Possible Issuer 3

Your recycleOffset is too large causing all the platforms to move too far ahead, out pacing the camera.

0 Replies

· Add your reply
  • Sort: 

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

How to clip game objects to a rectangular area? 0 Answers

Jumping from a floor to another floor (2D) 2 Answers

Accelerating speed 2 Answers

Character doesn't jump repeatedly 1 Answer

2d Beat Em Up Jump Functionality 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