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 BNOCSK · Nov 22, 2019 at 05:16 PM · instantiate prefabscrollingendless runner

Endless - Slight gaps that I cannot explain

Hi, I have been learning Unity and having a lot of fun with it.

I made an Endless Runner where the player moved along the Z axis and the track was instantiated further and further from origin - That was great and I had no discrepancies in position at all with that -Despite online reading that I would as it got further away from origin.

I thought I'd give it a go to make the track move towards me and re-instantiate and whilst it was a bit of a thought process to get it going, I have managed it.

My issues are:

  1. Firstly, I'd love to know if there's a tried and tested algorithm for figuring out the tile spawns when they are instantiated and moving towards the player - I don't have an algorithm and if I change tile lengths etc, I have to re figure it out, which isn't going well.

  2. Secondly, I am getting some small gaps, but only like every third tile.

I noticed that sometimes my tiles, even if hardcoded to spawn at 25f, would spawn at 24.8 or whatever.

Here is my script:

 public class TileManager : MonoBehaviour
 {
     public GameObject[] tilePrefabs;
     private Transform playerTransform;
 
     private float spawnZ = 5f;
     private float tileLength = 80f;
     private int tileCount = 4;
     private float laneScrollSpeed = -10f;
     private float trueSpawnZ;
 
     private int lastTileIndexUsed = 0;
 
     float maxSpeed = -50f;
 
     private List<GameObject> activeTiles;
 
     // Start is called before the first frame update
     private void Start()
     {
         // Where any tiles that aren't in the initial run should spawn
         trueSpawnZ = 235f;
         Debug.Log(trueSpawnZ);
         // Initialise List of Tiles currently active on screen
         activeTiles = new List<GameObject>();
         // Find player and assign it's position for use
         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
 
         // Initial Intiial Tiles
         for (int i = 0; i < tileCount; i++)
         {
             // Pass true to let it know it's the first set
             SpawnTile(true);
         }
     }
 
 
     // Update is called once per frame
     private void Update()
     {
         // If center of plane is past the center of the player
         // Let's make this the center of the plane + half so it's the top face that's behind the player
         Debug.Log((activeTiles[0].transform.position.z + 75.2f) + " Tile.Z + 75");
         if (activeTiles[0].transform.position.z + 80f <= playerTransform.transform.position.z)
         {
             SpawnTile();
             DeleteTile();
         }
     }
 
     void FixedUpdate()
     {
         // Control movement of the lane, (0, 0, scrollSpeed)
         transform.Translate(0, 0, laneScrollSpeed * Time.deltaTime);
     }
     int count = 0;
     private void SpawnTile(bool isFirstRun = false)
     {
         
         GameObject tile;
 
         // Instantiate a tile
         tile = Instantiate(tilePrefabs[RandomTileSpawn()]) as GameObject; //RandomTileSpawn()
         // Set the parent transform as it's own
         tile.transform.SetParent(transform);
         // Set the tile initial spawn position;
         tile.transform.position = new Vector3(0, 0, spawnZ);
         Debug.Log(tile.transform.position.z + " = Pos.Z on Created Object " + count);
         // If its the first run and we're generating 3 tiles, adjust the spawnz position
         if (isFirstRun)
         {
             spawnZ += tileLength;
         }
         else { spawnZ = trueSpawnZ; }
         activeTiles.Add(tile);
         count++;
     }
 
     private void DeleteTile()
     {
         Destroy(activeTiles[0]);
         activeTiles.RemoveAt(0);
     }
 
     private int RandomTileSpawn()
     {
         // Get the length of the collection 
         var totalTiles = tilePrefabs.Length;
         // Select a tile for use 
         var selectedTile = Random.Range(0, totalTiles);
         // Check the tile isn't the same one we just used, if it is, change it.
         while (selectedTile == lastTileIndexUsed)
         {
             // Try again
             selectedTile = Random.Range(0, totalTiles);
         }
         // Update the last used index.
         lastTileIndexUsed = selectedTile;
 
         // Return next index
         return selectedTile;
     }
 
 }



On the first run I create 4 tiles and as the track moves past the player, I am spawning a new tile at the end and deleting the one at [0].

Initial spawn is totally fine, this is where my issue comes in. My unfinished track (The length is 80f and the center point is 5f on each one - I don't know how to make it the actual center) alt text

The TileManager GameObject itself is at 0,0,0 alt text

I've basically got this all wrong somewhere, if anyone can shed some light on what I'm trying to do?

Thanks alot and if any more information is required, let me know and I'll get it asap!

issue2.jpg (40.9 kB)
issue1.jpg (35.6 kB)
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

186 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 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 Instantiate platforms with certain space inbetween each platform (Endless Runner) 0 Answers

Scrolling Animation Background For Endless Runner 0 Answers

my background is not scrolling why? 1 Answer

Scrollable world map with mouse drag or finger 0 Answers

Instantiating once in an update 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