- Home /
how to save last position for the player before he die
iwant code for save player position before die so when he die start with last position so idont have to restart the level from the beginning thank you.
Answer by andrew-lukasik · Jun 04, 2014 at 07:16 PM
Add to your player script like this one for example:
using UnityEngine;
using System.Collections;
public class RespawnMe : MonoBehaviour {
private static Vector3 respawnPosition = Vector3.zero;
private string levelWeAreIn = "";
void Start () {
if (levelWeAreIn!=Application.loadedLevelName) {
levelWeAreIn = Application.loadedLevelName;
respawnPosition = Vector3.zero;
}
if (respawnPosition==Vector3.zero) respawnPosition = transform.position;
else transform.position = respawnPosition;
}
void OnDestroy () {
respawnPosition = transform.position;
}
}
Answer by freshgamer10 · Jun 04, 2014 at 07:13 PM
Create a GameManager script or something along those lines, then create a public static Vector3 and access that variable when you die assigning it the transform.position of the player. Now you just need to access the value of that variable when you restart the level and you should be good to go. Remember to set a default value if you haven't died before.
Your answer
Follow this Question
Related Questions
How can I save and load a player's position? 5 Answers
Saving System? 1 Answer
How can playerprefs save my tower model and its position? 1 Answer
Help with Object position and state, save and load. 1 Answer
Save object position (random) 1 Answer