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 laurienash · Jul 03, 2016 at 12:05 AM · c#endless runnerrandomizescroller

Endless runner randomised scrolling terrain: don't want first terrain when scene loads to be randomised

Hi - I'm a bit stuck on how to continue with this script.

I'm making an endless runner - so have written this script which moves 2 active terrains forward, until they reach a certain position at which point the first terrain is set to inactive, and a new second one is set to active behind the other one. (to create a conveyor belt movement)

This is working fine - but I would like that each time this scene is loaded, the terrain1 is always the first object of the terrain array.

I thought I would set this in the Awake method:

 void Awake()
     {
         terrain1 = terrainMultiples[0];
     } 

But I've got stuck on working out how to not pick terrain1 in the Start method just the first time the scene is loaded.

Does anyone have an idea of how I should go about this?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TerrainMoverScaled : MonoBehaviour {
 
 public float moveForwards = 0f;
 GameObject terrain1;
 Vector3 targetPos = new Vector3 (0,0, -10);
 GameObject terrain2;
 public Speedometer speedGPS;
 public PerspectiveSwitchFinalSepCo persp;
 public TouchDetect touch;

 public GameObject [] terrainMultiples;
 private List<GameObject> terrainDeck;
 
 static public float speed = 15f;
 float lastspeed = 0f;
 float speed2 = 0f;
 public float targetSpeed = 0.0f;

 float changeSpeed;
 float w = 5f;
 float cutoff = 40f;
 bool high = false; 

 void Awake()
 {
     terrain1 = terrainMultiples[0];
 }

 void Start()
 {

     terrainDeck = new List<GameObject>(terrainMultiples);
     foreach (var t in terrainMultiples)
     {
         t.SetActive (false);
     }

     terrain1 = ChooseTerrain();
     terrain1.transform.position = new Vector3 (-6.85f, -3f, 33.9f);
     terrainDeck.Remove(terrain1);

     terrain2 = ChooseTerrain();
     terrain2.transform.position = new Vector3(-6.85f, -3f, 101.63f);
     terrainDeck.Remove(terrain2);

     terrain1.SetActive(true);
     terrain2.SetActive(true);
 }
 
 void Update () {
     
     
     //if (!high && speedGPS.speedInKmPerHour > cutoff+w )//highspeed
     if (Input.GetKeyDown ("w"))
     {
         persp.orthoToggle(false);
         
         targetSpeed = -0.5f;
         speed2=0;
         changeSpeed = 4f;
         high = true;
     }
     
     //if (high && speedGPS.speedInKmPerHour < cutoff-w)
     if (Input.GetKeyDown("e"))
     {
         persp.orthoToggle(true);
     
         targetSpeed = 0;
         speed2=0;    
         changeSpeed = 2f;
         high = false;
     }
     
     if (Mathf.Abs(moveForwards - targetSpeed) > 0.01f )    
         moveForwards = Mathf.SmoothDamp (moveForwards, targetSpeed, ref speed2, changeSpeed);

     else
         moveForwards = targetSpeed;
     
     terrain1.transform.Translate(Vector3.forward * Time.deltaTime * moveForwards * speed);
     terrain2.transform.Translate(Vector3.forward * Time.deltaTime * moveForwards * speed);
     
     lastspeed = speedGPS.speedInKmPerHour;
     
 }
 
 void LateUpdate()
 {
     if (targetPos.z - terrain1.transform.position.z > - 5f)
     {
         terrain1.SetActive(false);
         terrainDeck.Add(terrain1);
         terrain1 = ChooseTerrain();
         terrainDeck.Remove(terrain1);
         terrain1.transform.position = terrain2.transform.position + new Vector3(0.0f, 0.0f, +67.73f); 
         terrain1.SetActive(true);

     }
     
     else if (targetPos.z - terrain2.transform.position.z > - 5f)
     {
         terrain2.SetActive(false);
         terrainDeck.Add(terrain2);
         terrain2 = ChooseTerrain();
         terrainDeck.Remove(terrain2);
         terrain2.transform.position = terrain1.transform.position + new Vector3(0.0f, 0.0f, +67.73f);
         terrain2.SetActive(true);
     }
 }


 GameObject ChooseTerrain()
 {
     int index = Random.Range (0, terrainDeck.Count -1);
     return terrainDeck [index];
 }
 

}

Best, Laurien

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

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

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

I am trying to offset the camera from the player at all times even if the player moves. 0 Answers

How to call Random.insideUnitSphere.x Once 1 Answer

Spawning random tiles 1 Answer

Moving left and right with one button in 2d Game 1 Answer

need help in making an object pooler 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