Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Satori88 · Sep 06, 2017 at 02:28 PM · battle-system

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tormentoarmagedoom · Sep 11, 2017 at 11:39 AM 0
Share

upvote best awnser please

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Satori88 · Sep 06, 2017 at 06:30 PM 0
Share

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);
     
 }

}

avatar image tormentoarmagedoom Satori88 · Sep 08, 2017 at 06:57 AM 0
Share

@Satori88

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!

avatar image Satori88 tormentoarmagedoom · Sep 08, 2017 at 02:05 PM 0
Share

Thanks, you helped me a lot.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Side battle tutorial using 3d objects 0 Answers

Turn Base Battle Simulation/Autobattle 0 Answers

How do I do a for loop the won't go around everything? 1 Answer

Refresh gameObject to original prefab, but it isn't visible 1 Answer

How can I pause one object's Update() until another class has performed its task? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges