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
1
Question by Guennor · Mar 06, 2014 at 04:54 PM · procedural generationendless runnermodular

Endless "Falling" Game - Background generation

Hello. This is my first question here. I've seen some other questions about this topic but none of them helped me with my specific issue.

I want to make an endless falling game where you fall in a long, endless, vertical cave. First of all I want to know if it's viable to make it so the character falls with a rigidbody, instead of the background going up. This is really important, because it's going to be an android game, so this matters a lot. Also, people say that you can break the physics if a gameobject goes too far from its origin.

Then, about the background generation, how would I do it? I have some programming skills and tried a lot of things, but I have some problems on implementing a seamless background that generates when the player reaches the previous one.

![alt text][1] [1]: /storage/temp/23187-unity_question_endless_faller.png


I made this to illustrate better.

I want to know the best practice in terms of performance and seamlessness when generating the background. Do I use an array? Or an object to instantiate the background? People say that it's a bad thing to keep instantiating/destroying objects, so what could be done to avoid it?

I hope I was clear enough and thanks in advance!

unity_question_endless_faller.png (122.5 kB)
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 Dblfstr · Mar 07, 2014 at 06:41 PM 0
Share

How I do it, is I don't have the player actually move. I set the textureoffset (with a seamless texture) with time. So the texture scrolls on a plane as if the player was moving... nothing is actually moving. Though, your obstacles would need to move Up.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fifthknotch · Mar 06, 2014 at 05:19 PM

I made an endless runner using one seamless texture that scrolled on the plane. The plane would extend farther than the camera could see and because it scroll through one seamless texture, It looked like the player was actually running. Using a rigidbody to actually make your player fall would start eating up processing power and is probably not what you want to do on android. Another way of achieving the "look" of falling is to have multiple planes all moving upwards. Instead of instantiating and destroy just change the plane's transform position once it reaches a certain height to a height below the screen and make it move up. You could have in the script something like: if (transform.position.y >= "your value goes here") { transform.position.y -= "size of the plane"; } Either of these methods work but they both require the player not falling.

Comment
Add comment · Show 2 · 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 Guennor · Mar 07, 2014 at 06:33 PM 0
Share

I did this way but i'm having some problems. I made a script with a function that whenever the player hits a new plane, one other plane is chosen at random to be moved down (to the next "chunk" that will appear under the player). But I'm having some problems, whenever the player touches a plane, the plane he touches gets moved, ins$$anonymous$$d of a random one. I'm trying to work this out but if I solve this problem i'll be able to do this type of game the way you said. Thanks man!

avatar image fifthknotch · Mar 07, 2014 at 07:08 PM 0
Share

It sounds like some variable are getting flipped around. If you post the relevant code, we might could help you with this. Otherwise, good luck!

avatar image
0

Answer by Vitorcampea · Mar 06, 2014 at 05:56 PM

You will need 2 scripts, one for the background spawner and other for background

Background: (this will makin him move down at speed of 10 and destroy after the time you insert on inspecter)

 public class BackgroundMove : MonoBehaviour {
     public float speed = 10;
     public float timeToDestroy;
     public float currentTimeToDestroy;
 
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         currentTimeToDestroy = currentTimeToDestroy + 1 * Time.deltaTime;
         transform.Translate (0,-speed*Time.deltaTime,0);
         if (currentTimeToDestroy >= timeToDestroy) {
             Destroy(gameObject);
                 }
     }
 }


Background Spawner (this will spawn 1 background in each timespawn you insert on inspecter)

 using UnityEngine;
 using System.Collections;
 
 public class BackSpawner : MonoBehaviour {
     public GameObject background;
     public float timeToSpawn;
     public float currentTimeToSpawn;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         currentTimeToSpawn = currentTimeToSpawn+1 * Time.deltaTime;
         if (currentTimeToSpawn >= timeToSpawn) {
             Instantiate (background, transform.position, transform.rotation);
             currentTimeToSpawn = 0;
         }
     }
 }


this works and i tested it, good luck

Comment
Add comment · Show 5 · 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 Guennor · Mar 07, 2014 at 06:30 PM 0
Share

This way probably works but it keeps destroying and instantiating new gameobjects, so I don't think it's a optimal way to do it. But you gave me some ideas, thanks man!

avatar image Dblfstr · Mar 07, 2014 at 06:43 PM 0
Share

You can just use an objectpool to control the instantiate and destroy. So everything is Instantiated on Start(), and then made activated/deactivated through scripting by use of the Objectpool.

avatar image Guennor · Mar 07, 2014 at 07:16 PM 0
Share

Oh yeah I forgot I could do that here!

This script is attached to the player.

 void OnTriggerEnter2D(Collider2D hit)
     {
         if (hit.gameObject.tag == "touchSection");
         {
             int n = Random.Range(0, other.walls.Length);
             while (n == other.newNum)
             {
                 newNum = Random.Range(0, other.walls.Length);
             }
             other.$$anonymous$$ove(n);

         }//if
     }//OntriggerEnter

This one is attached to the object that moves the paths, or "chunks"

public class Path$$anonymous$$over : $$anonymous$$onoBehaviour {

 public GameObject[] walls;
 public Transform player;
 public float distance;
 public Vector3 pos;
 
 public int movespeed;

 public int oldNum;
 public int newNum;

 // Use this for initialization
 void Start () {
     pos = new Vector2(0, player.position.y - distance);
     newNum = Random.Range (0,walls.Length);
 }
 
 // Update is called once per frame
 void Update () {
     pos = new Vector2(0, player.position.y - distance);

     walls[0].transform.Translate(Vector2.up * Time.deltaTime * movespeed, Space.World);
     walls[1].transform.Translate(Vector2.up * Time.deltaTime * movespeed, Space.World);
     walls[2].transform.Translate(Vector2.up * Time.deltaTime * movespeed, Space.World);
     walls[3].transform.Translate(Vector2.up * Time.deltaTime * movespeed, Space.World);

     if(Input.GetAxis("Vertical") == 0)
     {  
         movespeed = 8;
     }
     if(Input.GetAxis("Vertical") > 0)
     {
         movespeed = 4;
     }
     if(Input.GetAxis("Vertical") < 0)
     {
     
     }//if
 }//update

 public void $$anonymous$$ove(int range)
 {
     newNum = range;
     while (oldNum == newNum)
     {
         newNum = Random.Range (0,walls.Length);
     }
     walls[newNum].transform.position = pos;

     oldNum = newNum;
 }//Spawn

}

Thanks in advance, man!

avatar image fifthknotch · Mar 08, 2014 at 03:38 AM 0
Share

I see your problem, you are sending move to the wall you hit at line 10 it the first script. You need to send the message to the one of the other walls. It looks like you tried to name the other wall "n", but ins$$anonymous$$d of sending a message to "n", you send the message to "other" with the parameter "n".

avatar image Guennor · Mar 11, 2014 at 03:48 PM 0
Share

Actually, I concealed that part. "other" is actually the other script:

  public class CallFunction : $$anonymous$$onoBehaviour {
     
         public Path$$anonymous$$over other;

Pathmover being the script that moves the walls (or "chunks" or "sections" or whatever you'd like to call it) I'm getting really confused about it, now. ):

I think the problem lies within this part:

 void OnTriggerEnter2D(Collider2D hit)
     {
         if (hit.gameObject.tag == "touchSection");
         {
             int n = Random.Range(0, other.walls.Length);
             while (n == other.newNum)
             {
                 newNum = Random.Range(0, other.walls.Length);
             }
             other.$$anonymous$$ove(n);
         }//if
     }//OntriggerEnter

I think that whenever the character hits the trigger of the wall section, it runs the line other.$$anonymous$$ove(n); a lot of times, so that's why ALL of the walls go down whenever the charactr touches one of them.

Here's the web player link for you to test... Left and right to move, not that it matters right now :P https://dl.dropboxusercontent.com/u/29752052/Arquivos/ProcDown/ProcDown.html

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

23 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

Related Questions

How do I proceduraly generate an environment around a sphere? 5 Answers

How to make a race track similar to audiosurf? 0 Answers

My game keeps freezing 1 Answer

How to Make an Endless Runner Move from track to track smoothly 0 Answers

Texture render performance - one 1024px, vs two 512px - for modular models? 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