You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.4 KiB

8 months ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
/// <summary>
/// This class is attached to the player object (prefab). It will call the GameState class
/// to set and get the player's position in a scene and place the player at that position.
/// </summary>
public class MapPosition : MonoBehaviour {
/// <summary>
/// When the player enters a scene, this method will query the GameState to get the player's
/// last know position in this scene. Then it will place the player object at that location.
/// </summary>
void Awake()
{
var lastPosition = GameState.GetLastScenePosition(SceneManager.GetActiveScene().name);
if (lastPosition != Vector3.zero)
{
transform.position = lastPosition;
}
}
/// <summary>
/// When the player leaves a scene, this method will save the player's position in the scene.
/// Scenes that are loaded with the Navigation Manager will cause an infinite loading loop, so
/// they will set the saveLastPosition flag to false and not save the position rather using a
/// configured starting position to place the player object.
/// </summary>
void OnDestroy()
{
if (GameState.saveLastPosition)
{
GameState.SetLastScenePosition(SceneManager.GetActiveScene().name, transform.position);
}
}
}