- Home /
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?
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.
@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".
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).
@LeFlop2001 Like how would I know if tile is offscreen || no longer relevant?
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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Help with clearing bugs in this C# script 1 Answer