Forum begins after the advertisement:
[Part 10] Camera not tracking player properly after dying from boss.
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 10] Camera not tracking player properly after dying from boss.
- This topic has 0 replies, 1 voice, and was last updated 2 weeks, 4 days ago by
Sean Ng.
-
AuthorPosts
-
March 18, 2026 at 12:51 pm #19305::
After dying to the boss and pressing the respawn button, the player change position but the camera is still bound to the boss room. The camera is still tracking the player but being bouded to boss room the player will not be able to be seen as they spawn outside the boss room.
To fix this the GameManager and CameraTrigger script will be modified.
CameraTrigger will add a variable to store the composite Collider, have a awake to add in the compositeCollider automatically. Lastly Add in a new function called RespawnSetCam(Collider2D playerCol), this helps checks if the player is within the camera bound when respawning.
public class CameraTrigger : MonoBehaviour { [SerializeField] CinemachineVirtualCamera newCamera; [SerializeField] Collider2D areaCovered; private void Awake() { areaCovered = GetComponent<CompositeCollider2D>(); } private void OnTriggerEnter2D(Collider2D _other) { if (_other.CompareTag("Player")) { CameraManager.Instance.SwapCamera(newCamera); } } public void RespawnSetCam(Collider2D playerCol) { Vector2 playerPos = playerCol.transform.position; Bounds areaBound = areaCovered.bounds; // Unfortunately playerCol bounds is not updated to respawn area but position is so checking and ensure the player is within the area bounds if (playerPos.x >= areaBound.min.x && playerPos.x <= areaBound.max.x && playerPos.y >= areaBound.min.y && playerPos.y <= areaBound.max.y) { CameraManager.Instance.SwapCamera(newCamera); } } }The GameManager script just need to modify the RespawnPlayer function a little, after the player respawn function called, get the player collider and all the cameraBounds in a array you’ll run through a foreach calling the RespawnSetCam function in each.
public void RespawnPlayer(float manaPenalty = 0f) { // If a benchScene is saved, it means that we benched somewhere. if (!string.IsNullOrEmpty(globalData.lastScene) && globalData.lastScene != SceneManager.GetActiveScene().name) //load the bench's scene if it exists. { SceneManager.LoadScene(globalData.lastScene); SavePoint sp = Bench.FindObjectBySaveID(globalData.benchSaveID) as SavePoint; if (sp) respawnPoint = sp.GetAnchorPosition(); } else { // Otherwise we spawn at the default respawn point. respawnPoint = defaultRespawnPoint; } PlayerController.Instance.transform.position = respawnPoint; UIManager.Instance.deathScreen.Deactivate(); PlayerController.Instance.Respawned(manaPenalty); // Ensuring the camera is bounded to the room the player respawns in Collider2D playerCollider = PlayerController.Instance.GetComponent<Collider2D>(); CameraTrigger[] camTriggerAreas = FindObjectsOfType<CameraTrigger>(); foreach (CameraTrigger camTrigger in camTriggerAreas) { camTrigger.RespawnSetCam(playerCollider); } } -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: