I’ve 2 scenes, an open world scene, and a battle scene. When my character touches an enemy (collision), the 3rd scene starts. When he beats the enemy, he returns to 2nd scene, but the position he was when it started. I want to know how can my character keep the position he was when he touched the enemy.
@Satori88 , You need to save the transform.position when the first collision happen. Then save it in a script placed in a GameObject that remains loaded when scene changes. And after the battle, only need to load the saved transform.position.
For do this first, you need to use this:
DontDestroyOnLoad(gameObject);
in a Script placed in the GameObject that will remain loaded. Lets say then RemainingGameObject have a component script called RemainingScript.
When the battle is going to commence, before changing scene, you need to save in a Vector3 variable the character.transform.position in RemainingScript. After this, you can load the new Scene. Try it, you will see the RemainingGameObject is still in the new Scene.
So, when finish the battle, RemainingObject will be loaded again in the open world scene.
So… always you open the world scene, you must load the position of the character from this script (even the 1st time is loaded, just put the initial position in the RemainingScript for the 1st time you load the scene, and it will change every time a battle commence).
But, when editing the game, if you place the RemainingObject at the openworld scene, you will have a little “problem”. When you load the open world for 2nd time, it will load the RemainingObject from battle scene, and will create a new RemainigObject.
To solve this, use some kind of detector for other RemainingObject in the scene. Crate a Tag for RemainingObject, create a bool variable and do this:
private bool thisisthecorrectone = false;
if (GameObject.FindGameObjectsWithTag("RemainingObject").Length > 1 && thisisthecorrectone == false)
{
Destroy(gameObject);
}
else thisisthecorrectone = true;
Then, if there is only one RemainingObject, it means is the 1st time you load the scene, so it will become the correct one. All other times you load the scene, the new RemainingObject will be deleted.
If this dont help, ask me more, i will help you