C
C
Cetar2021-10-29 13:05:38
Unity
Cetar, 2021-10-29 13:05:38

There is a 2D Runner Vertical It is necessary that the walls go down and as soon as they go down new ones appear with prepared levels, how to do this?

There is a 2D Runner Vertical It is necessary that the walls go down and as soon as they go down new ones appear with prepared levels, how to do this?
1 Script
public class DownMover : MonoBehaviour
{
public float speed;
private Spawner spawner;
private bool spawned;
private void Start()
{
spawner = FindObjectOfType();
speed = spawner.speed;
}


private void Update()
{
transform.Translate(Vector2.down * speed * Time.deltaTime);
speed += spawner.speedIncrease * Time.deltaTime;
if(transform.position.y < 0 && !spawned)
{

}
}
}
2Script
public class Spawner : MonoBehaviour
{
public GameObject[] block;

public float speed;
public float speedIncrease;
void Start()
{

}


private void Update()
{
speed += speedIncrease * Time.deltaTime;
}
public void SpawnWave()
{

int rand = Random.Range(0, block.Length);
Instantiate(block[rand], transform.position, Quaternion.identity);
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nekit Medvedev, 2021-11-01
@Cetar

Alternatively, make a trigger below the camera. If objects fall into the trigger, they are destroyed, and new ones are created from above. Let's say all objects are cubic, then we can make procedural generation based on cellular automata, set such rules so that the player always has a path. Also, for optimization, you can make a pool of blocks (their maximum number on the screen) and not destroy / create, but activate / deactivate. This will happen faster. The movement of blocks, it is desirable to register in FixUpdate and make a fixed shift for all objects.
The trigger under the camera must be done taking into account this shift, so that the blocks do not jump over it. Or calculate by the height of the screen and the speed of movement.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question