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 EinsteinsBrother · Jan 22, 2017 at 09:52 PM · 2d gameendless runnerverticalstudent

Trying to create an endless vertical level

Hi guys, I got to code an app for my school project. I'm trying to learn how to code in C# through following this tutorial: https://www.raywenderlich.com/69544/make-game-like-jetpack-joyride-unity-2d-part-2 . In the tutorial, although a endless level is created, it's horizontal, opposed to what I'm looking for. So I took the code and modified it, that as far as I know, it should work, but it does not. Might be important to know, that the player moves, not the background. Here is my code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class GeneratorScript : MonoBehaviour {
     public GameObject[] availableRooms;

 public List<GameObject> currentRooms;

 private float screenHeightInPoints;
 // Use this for initialization
 void Start () {
     screenHeightInPoints = 2.0f * Camera.main.orthographicSize;
 }
 void GenerateRoomIfRequired()
 {

     List<GameObject> roomsToRemove = new List<GameObject>(); 


     bool addRooms = true;        // will be set false most of the time in the for part


     float playerY = transform.position.y; // getting the players position


     float removeRoomY = playerY - screenHeightInPoints;      // the point a room should be removed (isn't seen by the player);  


     float addRoomY = playerY + screenHeightInPoints;  // the point a room should be added


     float farthestRoomEndY = 0;

     foreach(var room in currentRooms)
     {

         float roomHeight = room.transform.FindChild("bg").localScale.y + room.transform.FindChild("bf_window").localScale.y; // setting the room height by adding the heigth of both parts of the room
         float roomStartY = room.transform.position.y - (roomHeight * 0.5f);    //setting the start of a room
         float roomEndY = roomStartY + roomHeight;    //setting the end of a room                        


         if (roomStartY > addRoomY)
             addRooms = false; // no room should be added


         if (roomEndY < removeRoomY)
             roomsToRemove.Add(room); // room should be deleted


         farthestRoomEndY = Mathf.Max(farthestRoomEndY, roomEndY); // setting the end of the room
     }


     foreach(var room in roomsToRemove)
     {
         currentRooms.Remove(room); //destroying the rooms
         Destroy(room);            
     }


     if (addRooms)
         AddRoom(farthestRoomEndY); //adding the room
 }
 void AddRoom(float farhtestRoomEndY)
 {

     int randomRoomIndex = Random.Range(0, availableRooms.Length); //choosing a random prefab ot of the availableRooms array


     GameObject room = (GameObject)Instantiate(availableRooms[randomRoomIndex]); //creating that prefab


     float roomHeight = room.transform.FindChild("bg").localScale.y + room.transform.FindChild("bg_window").localScale.y; //getting the height of both textures to set the room height


     float roomCenter = farhtestRoomEndY + roomHeight * 0.5f; // setting the center height of the room one wants to add, adding half the room height to the end of the room


     room.transform.position = new Vector3( 0, roomCenter, 0); // moving the new room on top of the old room


     currentRooms.Add(room); // storing the newly created room in the currentRooms array     
 }


 void FixedUpdate () 
 {    
     GenerateRoomIfRequired();
 }

}

On top, I read that it's better to simply move the rooms instead of destroying and creating them, does someone have an idea or even solution on that? Excuse my English, I'm just a student from Germany. Thanks in advance.

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

2 Replies

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

Answer by GarretLawrence · Jan 23, 2017 at 08:24 AM

I have made a simple demo for you. Try it

Use Left/Right Arrow key to move Left/Right.

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 EinsteinsBrother · Jan 23, 2017 at 08:51 PM 0
Share

Thanks a lot, but could you briefly explain how to set this demo up? Thanks in advance.

avatar image GarretLawrence EinsteinsBrother · Jan 23, 2017 at 11:25 PM 0
Share

Video guide

Good luck :D

avatar image EinsteinsBrother GarretLawrence · Jan 24, 2017 at 07:32 PM 0
Share

I'm so sorry and really don't want to annoy you, but when I try to attach the script, the editor tells me there's an error with the following line of code:

 lastSpawnedBG = Instantiate(bgList[Random.Range(0, bgList.Count)], spawnPos,Quaternion.identity);

It tells me: "Assets/EndlessRun/EndlessRun.cs(36,13): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)".

Do you have a solution for that? Really sorry for annoying you like that for such a simple question. Thanks in advance. (Hopefully the last time)

Show more comments
avatar image EinsteinsBrother · Jan 29, 2017 at 05:34 PM 0
Share

This works perfectly, when set up in a 3D Project with normal BoxCollider and RigidBodys (thanks a lot), but when I try to set it up in a 2D Project with 2D BoxCollider and RigidBody2D it somehow doesn't work. Do you have an idea about how to solve this? Thanks in advance.

avatar image
0

Answer by srikaran_p · Jan 23, 2017 at 12:24 PM

Just do whatever you have been doing on the x - axis to y-axis.
Like make the player move from down to up.
Secondly, if you move the level then you would get a pattern of obstacles which would make the game boring. So, creating and destroying obstacles randomly would be great.
Finally, instantiate your rooms (or obstacles) on the y-axis.
Personally, I don't like to modify codes because that is a long process. Why don't you try to make your own code so that you would know what each statement does. All the Best.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

4 C# errors Endless Runner 0 Answers

Jitter on 2d movement along the y-axis 1 Answer

How to add triple jump when i collect power-up 2 Answers

How to make lane switching in mobile endless runner using UI buttons? 1 Answer

Instantiating randomly sized prefab to the right of another 2D 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