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 /
  • Help Room /
avatar image
0
Question by rodude123 · Mar 04, 2016 at 03:53 PM · 2d2d-platformerbackgroundendless runnerrooms

Endless runner

I have this script which generates the scene, but I need some help changing the background based on the amount of points that the player has. It would be great if someone could help, I have been told by many people to try it my self but I have been trying something for an hour now, so please don't say have you tried it or anything else. Thanks in advance

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GeneratorScript : MonoBehaviour {
 
     public GameObject[] availableRooms;
     
     public List<GameObject> currentRooms;
     
     private float screenWidthInPoints;
 
     public GameObject[] availableObjects;    
     public List<GameObject> objects;
     
     public float objectsMinDistance = 5.0f;    
     public float objectsMaxDistance = 10.0f;
     
     public float objectsMinY = -1.4f;
     public float objectsMaxY = 1.4f;
     
     public float objectsMinRotation = -45.0f;
     public float objectsMaxRotation = 45.0f; 
 
 
     // Use this for initialization
     void Start () {
         float height = 2.0f * Camera.main.orthographicSize;
         screenWidthInPoints = height * Camera.main.aspect;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void FixedUpdate () {
         
         GenerateRoomIfRequred();
 
         GenerateObjectsIfRequired();    
     }
 
 
     void AddRoom(float farhtestRoomEndX)
     {
         //1
         int randomRoomIndex = Random.Range(0, availableRooms.Length);
         
         //2
         GameObject room = (GameObject)Instantiate(availableRooms[randomRoomIndex]);
         
         //3
         float roomWidth = room.transform.FindChild("floor").localScale.x;
         
         //4
         float roomCenter = farhtestRoomEndX + roomWidth * 0.5f;
         
         //5
         room.transform.position = new Vector3(roomCenter, 0, 0);
         
         //6
         currentRooms.Add(room);            
     } 
 
     void GenerateRoomIfRequred()
     {
         //1
         List<GameObject> roomsToRemove = new List<GameObject>();
         
         //2
         bool addRooms = true;        
         
         //3
         float playerX = transform.position.x;
         
         //4
         float removeRoomX = playerX - screenWidthInPoints;        
         
         //5
         float addRoomX = playerX + screenWidthInPoints;
         
         //6
         float farhtestRoomEndX = 0;
         
         foreach(var room in currentRooms)
         {
             //7
             float roomWidth = room.transform.FindChild("floor").localScale.x;
             float roomStartX = room.transform.position.x - (roomWidth * 0.5f);    
             float roomEndX = roomStartX + roomWidth;                            
             
             //8
             if (roomStartX > addRoomX)
                 addRooms = false;
             
             //9
             if (roomEndX < removeRoomX)
                 roomsToRemove.Add(room);
             
             //10
             farhtestRoomEndX = Mathf.Max(farhtestRoomEndX, roomEndX);
         }
         
         //11
         foreach(var room in roomsToRemove)
         {
             currentRooms.Remove(room);
             Destroy(room);            
         }
         
         //12
         if (addRooms)
             AddRoom(farhtestRoomEndX);
     }
 
     void AddObject(float lastObjectX)
     {
         //1
         int randomIndex = Random.Range(0, availableObjects.Length);
         
         //2
         GameObject obj = (GameObject)Instantiate(availableObjects[randomIndex]);
         
         //3
         float objectPositionX = lastObjectX + Random.Range(objectsMinDistance, objectsMaxDistance);
         float randomY = Random.Range(objectsMinY, objectsMaxY);
         obj.transform.position = new Vector3(objectPositionX,randomY,0); 
         
         //4
         float rotation = Random.Range(objectsMinRotation, objectsMaxRotation);
         obj.transform.rotation = Quaternion.Euler(Vector3.forward * rotation);
         
         //5
         objects.Add(obj);            
     }
 
     void GenerateObjectsIfRequired()
     {
         //1
         float playerX = transform.position.x;        
         float removeObjectsX = playerX - screenWidthInPoints;
         float addObjectX = playerX + screenWidthInPoints;
         float farthestObjectX = 0;
         
         //2
         List<GameObject> objectsToRemove = new List<GameObject>();
         
         foreach (var obj in objects)
         {
             //3
             float objX = obj.transform.position.x;
             
             //4
             farthestObjectX = Mathf.Max(farthestObjectX, objX);
             
             //5
             if (objX < removeObjectsX)            
                 objectsToRemove.Add(obj);
         }
         
         //6
         foreach (var obj in objectsToRemove)
         {
             objects.Remove(obj);
             Destroy(obj);
         }
         
         //7
         if (farthestObjectX < addObjectX)
             AddObject(farthestObjectX);
     }
 }
 
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 tanoshimi · Mar 04, 2016 at 06:51 PM 0
Share

It's very hard to help you from the problem description and code attached. What do the numeric comments mean? In what way do you want to "change the background"?

1 Reply

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

Answer by CosmicKitty · Mar 05, 2016 at 01:01 AM

seems like it would just be an easy if statement? if points > x , then background = blue.... etc?

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

64 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 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 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

Scrolling background wall collision problem 0 Answers

Object pooling system. Objects aren't spawning 0 Answers

Walking around 2D planets? 1 Answer

How to get custom data from a tile 0 Answers

How to make so that the player can jump on a object, but he can't stand on it? (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