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

Conteúdo do Curso

Fighting Game in Unity

Fighting Game in Unity

1. Unity Animation System
2. Player Behavior
3. Enemy Behavior
4. Improve the Game
5. Improve the Flappy Bird Game

Random Generated Background.

Camera Follow the Player:

The CameraFollow class makes the camera follow the player along the x-axis while maintaining the camera's initial y and z positions.

Methods:

Start Method:

Purpose: Initializes the initialPosition variable with the camera's starting position when the game begins.

LateUpdate Method:

Purpose: Updates the camera's position to follow the player's x-axis movement.

How It Works: initialPosition.x = player.transform.position.x; updates the x-coordinate of initialPosition to match the player's x-coordinate, and transform.position = initialPosition; sets the camera's position to the updated initialPosition, ensuring it follows the player along the x-axis while maintaining the original y and z positions.

Explanation of the ParallaxBackground:

The ParallaxBackground class makes a background element, like clouds, repeat infinitely to create a parallax effect in a 2D game. The background will reposition itself when the player exits its bounds, giving the illusion of an endless background.

Methods:

Start Method:

Purpose: Initializes the width variable with the width of the background element.

How It Works: GetComponent<BoxCollider2D>() retrieves the BoxCollider2D component attached to the background element, and .bounds.size.x gets the width of the collider's bounding box, which is then stored in the width variable for later use.

OnTriggerExit2D Method:

Purpose: Repositions the background element when the player exits its bounds, creating the illusion of an infinite scrolling background.

How It Works: When the player exits the background element's trigger collider (OnTriggerExit2D(Collider2D collision)), it verifies if the collider belongs to the player (if(collision.tag == "Player")). It adjusts the background element's position to scroll seamlessly by shifting it right by twice its width (position.x += width * 2f;). This adjustment ensures the background element reappears smoothly after its paired element scrolls out of view.

After calculating the new position, the script updates the background element's position (transform.position = position;). This mechanism allows the background to maintain a continuous scrolling effect in sync with the player's movements within the game environment.

Summary:

  • Initialization: The width of the background element is calculated and stored when the game starts;
  • Repositioning: When the player exits the bounds of the background element, the element's position is shifted to the right by twice its width, creating a continuous looping effect.

This setup gives the illusion of an endless parallax background by repeatedly repositioning the background elements as the player moves, ensuring a smooth and continuous visual experience.

What happens to the object's position when the player exits its trigger collider?

Selecione a resposta correta

Tudo estava claro?

Seção 5. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt