Random Generated Background.
- , opens subtitles settings dialogsubtitles settings
- subtitles off
- , selectedEnglish Captions
- 2x
- 1.5x
- , selected1x
- 0.5x
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
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:
csharp91234private void Start(){initialPosition = transform.position;}
Purpose: Initializes the initialPosition
variable with the camera's starting position when the game begins.
LateUpdate Method:
csharp912345private void LateUpdate(){initialPosition.x = player.transform.position.x;transform.position = initialPosition;}
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:
csharp91234private void Start(){width = GetComponent<BoxCollider2D>().bounds.size.x;}
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:
csharp9123456789private void OnTriggerExit2D(Collider2D collision){if(collision.tag == "Player"){var position = transform.position;position.x += width * 2f;transform.position = position;}}
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.
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione