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
0
Question by conguerror · Jan 10, 2021 at 03:33 PM · c#spawntriggersdetectionendless runner

Spawn on trigger

So, I am creating an endless runner game and I should spawn some tiles. I decided to use the trigger enter method.This code snippet goes into the each individual platform script,so I instantiate.

    private void OnTriggerEnter(Collider other)
         {
             if(other.tag == "Player")
             {
                          
                 FindObjectOfType<TileManager>().shouldChange = true;            
                 StartCoroutine(WaitAndChange());
             }
         }
 IEnumerator WaitAndChange()
     {
         yield return new WaitForSeconds(waitFor);
         FindObjectOfType<TileManager>().DeleteTile();
     }

This code snippet goes into the tile manager. Tiles move towards player not vice versa, so illusion of movement is created and everything is optimized

 private void LateUpdate()
     {
         if(gameStartScript.gameStarted && playerController.isAirborn == false)
         {
             //if (gameStartScript.GetFirstTile().GetComponent<RoadStart>().shouldMove == true)
             //{
                 gameStartScript.GetFirstTile().transform.Translate(gameStartScript.GetFirstTile().transform.forward * playerController.speed * Time.deltaTime);
             //}
             foreach (var tile in activeTiles)
             {
                 tile.transform.Translate(tile.transform.forward * playerController.speed * Time.deltaTime);
             }
         }
     }

This code snippet is connected to platform, so when I enter the platform's trigger there is a switch and I tile is created

 if (shouldChange)
         {            
             SpawnTile();
             shouldChange = false;//so i create tile once
             //SpawnTile();           
         }

Tile creation function: I instantiate a random tile from list and attach tile at the end of the last instantiated tile. It works fine if speed is not too high

 GameObject tile = Instantiate(prefabs[Random.Range(0, prefabs.Count)], new Vector3(activeTiles[activeTiles.Count - 1].transform.position.x + xAdditive, gameStartScript.GetFirstTile().transform.position.y, activeTiles[activeTiles.Count - 1].transform.position.z + tileLength), gameStartScript.GetFirstTile().transform.rotation);           


You might wonder: what is the problem?So when I set the player's speed or some time passes (which is actually how fast ground is scrolled) a bit high then tiles don't instantiate. They don't, and player just falls off the grid. What is wrong with these scripts?

Comment
Add comment · Show 9
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 conguerror · Jan 10, 2021 at 03:46 PM 0
Share

Any other date will be provided if needed

avatar image conguerror · Jan 10, 2021 at 03:48 PM 0
Share

Hope someone answers

avatar image LeFlop2001 · Jan 10, 2021 at 03:58 PM 0
Share

it could simply be that your player moves so fast that just warps past the trigger without touching it, witch really depends on how big the triggers are and how fast the player is moving. the fix would be to change the collision type.

However using trigger for such a task is probably not needed. Your Tilemanager could simply spawn and delete tiles if the distance to the peek tile of the queue is to great.

avatar image conguerror LeFlop2001 · Jan 10, 2021 at 04:26 PM 0
Share

@LeFlop2001 Can you PLEASE give a code of that "if statement"? Please note that tiles are the one that are moving not the player. I don't know how to make that "if statement".

avatar image LeFlop2001 conguerror · Jan 10, 2021 at 04:31 PM 1
Share
 if(Vector2.Distance(tiles.Peek() ,player.transform.position > distance)
 {
     tiles.Dequeue().Destroy();
     tiles.Enqueue(Spawn());
 }

This might be a bit confusing if youre not familiar with queues (in which case im happy to explain).

Show more comments
avatar image conguerror · Jan 11, 2021 at 07:31 AM 0
Share

@LeFlop2001 Like how would I know if tile is offscreen || no longer relevant?

1 Reply

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

Answer by conguerror · Jan 11, 2021 at 02:00 PM

https://www.youtube.com/watch?v=4MOEZW-ZjSQ

I pretty much liked this video. Hopefully it helps you too.

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

137 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Help with clearing bugs in this C# script 1 Answer

(C#) Help with spawning a random GameObject 1 Answer

How to reset netID that wasn't spawned 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