- Home /
After battle: keep character's position
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.
Answer by tormentoarmagedoom · Sep 06, 2017 at 05:32 PM
@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 :D
Could you show me vector3 code? I'm new at this... This is my RemainingScript:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class $$anonymous$$eepPos : $$anonymous$$onoBehaviour {
void Awake()
{
DontDestroyOnLoad(gameObject);
}
}
If you are new at scripting, maybe you should take a look at tutorial videos.
I explain you a little: A vector3 variable, is the variable type used for deter$$anonymous$$ate positions in world space (and other things) but you need it for position. It have 3 float elements, separeted by comas (x, y, z).
AS the same with all variables, first, you have to declare it, for example this:
Vector3 CharacterPosition;
Then Initiate the variable:
CharacterPosition = new Vector3 (10f,20f,30f);
for example if you want to move the GameObject "Qwert" to x=10 y =20 y=30 , you need to:
Qwerty.transform.position = new Vector3 (10,20,30);
or this:
Qwerty.transform.position = CharacterPosition
As you can see, CharacterPosition is already a Vector3 variable, so you can use it directly. There are some prefab vector3, for example Vector3.zero (wich means (0,0,0).
Always you want to store or load a position, you need to operate with Vector3 variables.
If you need help with declaring/reading/changing variables between 2 scripts, tell me!
Your answer
Follow this Question
Related Questions
Need help making a pokemon turn based system ! 1 Answer
2D Battle System In a 3D Game 3 Answers
Classic turn-based RPG battle scene question 1 Answer
Turn Order 2 Answers