Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Random Generated Obstacles. | Improve the Flappy Bird Game
Fighting Game in Unity

bookRandom Generated Obstacles.

The Obstacle class manages obstacles in the game, ensuring they reposition themselves when they go off-screen to maintain continuous gameplay.

Methods

Start Method

private void Start()
{
    lastObject = startingLastObject;
    player = FindAnyObjectByType<PlayerMVT>().transform;
}

Purpose: Initializes the last obstacle and finds the player's transform.

How It Works: lastObject = startingLastObject;: Sets the lastObject to the initial obstacle specified in the Inspector; player = FindAnyObjectByType<PlayerMVT>().transform;: Finds the player's transform using FindAnyObjectByType method.

OnBecameInvisible Method

private void OnBecameInvisible()
{
    if (player == null) return;
    if (transform.position.x >= player.position.x) return;
    if (transform == lastObject) return;

    Vector2 position = transform.position;
    float random = Random.Range(0, 100);

    if (random <= 50)
    {
        position.y = Random.Range(-2, 1);
    }
    else
    {
        position.y = Random.Range(15, 18);
    }

    position.x = lastObject.position.x + Random.Range(6f, 10f);
    transform.position = position;
    lastObject = transform;
}

Purpose: Repositions the obstacle when it goes off-screen and meets certain conditions.

How It Works: The script ensures the player exists before proceeding (if (player == null) return;), verifies the obstacle is positioned behind the player (if (transform.position.x >= player.position.x) return;), and prevents consecutive repositioning (if (transform == lastObject) return;).

It retrieves the current obstacle position (Vector2 position = transform.position;), generates a random number (float random = Random.Range(0, 100);), and adjusts the obstacle's y-position based on the random number.

Then, it updates the x-position relative to the last obstacle (position.x = lastObject.position.x + Random.Range(6f, 10f);), and updates lastObject to refer to the current obstacle (lastObject = transform;).

Summary

  • Initialization: Sets the initial last obstacle and finds the player;
  • Repositioning: When an obstacle goes off-screen and meets specific conditions, it is repositioned at a random distance ahead of the last obstacle with a random height;
  • Conditions: Ensures obstacles are only repositioned if they are behind the player, and not the same obstacle repeatedly.

This setup allows for a continuous flow of obstacles in the game, maintaining challenge and variety for the player.

question mark

What determines the new y-position of the object when it becomes invisible and is repositioned?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 5. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 3.33

bookRandom Generated Obstacles.

Sveip for å vise menyen

The Obstacle class manages obstacles in the game, ensuring they reposition themselves when they go off-screen to maintain continuous gameplay.

Methods

Start Method

private void Start()
{
    lastObject = startingLastObject;
    player = FindAnyObjectByType<PlayerMVT>().transform;
}

Purpose: Initializes the last obstacle and finds the player's transform.

How It Works: lastObject = startingLastObject;: Sets the lastObject to the initial obstacle specified in the Inspector; player = FindAnyObjectByType<PlayerMVT>().transform;: Finds the player's transform using FindAnyObjectByType method.

OnBecameInvisible Method

private void OnBecameInvisible()
{
    if (player == null) return;
    if (transform.position.x >= player.position.x) return;
    if (transform == lastObject) return;

    Vector2 position = transform.position;
    float random = Random.Range(0, 100);

    if (random <= 50)
    {
        position.y = Random.Range(-2, 1);
    }
    else
    {
        position.y = Random.Range(15, 18);
    }

    position.x = lastObject.position.x + Random.Range(6f, 10f);
    transform.position = position;
    lastObject = transform;
}

Purpose: Repositions the obstacle when it goes off-screen and meets certain conditions.

How It Works: The script ensures the player exists before proceeding (if (player == null) return;), verifies the obstacle is positioned behind the player (if (transform.position.x >= player.position.x) return;), and prevents consecutive repositioning (if (transform == lastObject) return;).

It retrieves the current obstacle position (Vector2 position = transform.position;), generates a random number (float random = Random.Range(0, 100);), and adjusts the obstacle's y-position based on the random number.

Then, it updates the x-position relative to the last obstacle (position.x = lastObject.position.x + Random.Range(6f, 10f);), and updates lastObject to refer to the current obstacle (lastObject = transform;).

Summary

  • Initialization: Sets the initial last obstacle and finds the player;
  • Repositioning: When an obstacle goes off-screen and meets specific conditions, it is repositioned at a random distance ahead of the last obstacle with a random height;
  • Conditions: Ensures obstacles are only repositioned if they are behind the player, and not the same obstacle repeatedly.

This setup allows for a continuous flow of obstacles in the game, maintaining challenge and variety for the player.

question mark

What determines the new y-position of the object when it becomes invisible and is repositioned?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 5. Kapittel 3
some-alt