- In the scene named Stage1, the Player moves to the scene named Stage1_1 through a Gate.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangeSceneOnTag : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
// When it contacts an object with the "Star" tag
if (other.CompareTag("Star"))
{
LoadStage1_1Scene();
}
}
void LoadStage1_1Scene()
{
SceneManager.LoadScene("Stage1_Scene 1");
}
}
- From Stage1_1, the Player moves to a specific SpawnPoint in Stage1 through another Gate.
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangeSceneOnTag2 : MonoBehaviour
{
private void Start()
{
}
private void OnTriggerEnter2D(Collider2D other)
{
// When it contacts an object with the "Gate" tag
if (other.CompareTag("Gate"))
{
LoadStage1Scene();
// Register a method to be called when the scene load is completed
SceneManager.sceneLoaded += OnSceneLoaded;
}
}
void LoadStage1Scene()
{
SceneManager.LoadScene("Stage1_Scene");
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (scene.name == "Stage1_Scene")
{
GameObject player = GameObject.FindGameObjectWithTag("Player");
GameObject spawnPoint = GameObject.Find("SpawnPoint");
if (player != null && spawnPoint != null)
{
player.transform.position = spawnPoint.transform.position;
Camera mainCamera = Camera.main;
if (mainCamera != null)
{
mainCamera.transform.position = new Vector3(spawnPoint.transform.position.x, spawnPoint.transform.position.y, mainCamera.transform.position.z);
}
// Reset the ParallaxBackground.
ResetParallaxBackground();
}
// Remove this event handler as it's no longer needed.
SceneManager.sceneLoaded -= OnSceneLoaded;
}
}
void ResetParallaxBackground()
{
ParallaxBackground[] parallaxBackgrounds = FindObjectsOfType<ParallaxBackground>();
foreach (var bg in parallaxBackgrounds)
{
bg.ResetBackground(Camera.main.transform.position);
}
}
}
The issue I’m facing is with the Background. I have a code named ParallaxBackground.cs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParallaxBackground : MonoBehaviour
{
public Transform cameraTransform;
public float parallaxSpeed = 0.5f; // Adjust the value between 0 and 1 to change the degree of movement.
private Vector3 lastCameraPosition;
void Start()
{
lastCameraPosition = cameraTransform.position;
}
void Update()
{
Vector3 deltaMovement = cameraTransform.position - lastCameraPosition;
transform.position += new Vector3(deltaMovement.x * parallaxSpeed, deltaMovement.y * parallaxSpeed, 0f);
lastCameraPosition = cameraTransform.position;
}
public void ResetBackground(Vector3 newCameraPosition)
{
lastCameraPosition = newCameraPosition;
}
}
However, when moving to the specific SpawnPoint in Stage1, this code does not function. As a result, the original background of Stage1 does not appear. How do I solve it?
Stage1 -> Stage1_1 -> SpawnPoint in Stage1 ==> Scene transition is OK.
However, ParallaxBackground.cs associated with the scene transition does not work.
The Player moved to the SpawnPoint, but the background did not follow him.
im not sure your onscene load runs.. nothing suggests it will live beyond the scene change