Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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
0
Question by impurekind · Dec 07, 2018 at 12:30 AM · directionpauseforwardquaternionsvectors

How can I change player position and save direction?

I'm going to jump my player to a new position in the level when I pause the game, which I will use as the pause screen/room, and I want to store their current position and direction as I press pause so when I resume the game they will be in the same place and facing the same way they were when I pressed pause, even if I move and turn around while in the pause screen/area.

So how do I store and set the player's direction properly?

I've tried using the following when the pause button is pressed down:

 if (!gamePaused)
             {
                 playerStoredPosition = player.transform.position; // Stores the position of player in the level
                 playerStoredDirection = player.transform.forward; // Stores the forward direction of player in the level
 
                 player.transform.position = playerPausePosition; // Jumps player to pause position (set in Start)
                 player.transform.forward = playerPauseDirection; // Turns player to pause direction (set in Start)
 
                 gamePaused = true;
             }
             else
             {
                 player.transform.position = playerStoredPosition; // Returns player to stored position
                 player.transform.forward = playerStoredDirection; // Returns player to stored direction
 
                 gamePaused = false;
             }

But it's not working.

The storing of the position works fine, but when I resume the game the direction the player is facing is wherever I've turned when in the pause room rather than where I was facing when I actually pressed pause.

Any ideas how to get this to work properly?

Comment
Add comment · Show 8
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 Vega4Life · Dec 07, 2018 at 12:49 AM 0
Share

I am a little confused. Where is 'playerPausedPosition' and 'playerPauseDirection' being set?

avatar image impurekind Vega4Life · Dec 07, 2018 at 01:02 AM 0
Share

Currently at the top of the script in the Start function:

 void Start()
     {
             playerPausePosition = new Vector3(0f, 0.01f,-102f);
             playerPauseDirection = new Vector3(0f, 0f, 0f);
     }

I'm basically just trying to store/set the player's direction to 0 facing forward by default (presumably in the world axis) when I pause the game.

I've also tried using public variables and setting them in the inspector, and also quaternions and localRotation too, and that doesn't help either.

Note: I'm using Unity's built in PlayerController for the player (and the Oculus one when in VR mode), which I worry might be overriding my code or something.

avatar image Vega4Life impurekind · Dec 07, 2018 at 02:18 AM 0
Share

How is the original code you posted called? OnClick()? Then does an if check if the game is paused? Or is that in an update?

Show more comments
avatar image nicholasw1816 · Dec 10, 2018 at 02:05 PM 0
Share

You say you can store position but not rotation? Create a quaternion variable in your script, then store the players rotation in the quaternion. Example

 private Quaternion myRotation;
 
 void SomeFunction()
 {
     myRotation = transform.rotation;
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by darkStar27 · Dec 07, 2018 at 04:32 AM

You can try passing the game with

 Time.timeScale = 0;

This will solve your problem as you do not need to store the player position and directions and whenever you need to unpause just set it to 1 again the game will resume from the last paused frame.

For more details on what is Time.timeScale and how to use it see here.

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 impurekind · Dec 07, 2018 at 05:15 AM 0
Share

I can't do this unfortunately because I actually need the player to still be able to move and turn and shoot and stuff when in the pause screen, as the pause screen is just another room directly in the game where the player shoots the various options, which is particularly suited for when in VR mode.

Also, I did try the timescale stuff and it was pretty cool for just pausing most of the game in general but it caused some of its own issues along with the player stuff I mentioned.

avatar image darkStar27 impurekind · Dec 10, 2018 at 04:27 AM 0
Share

What you can do is when the player presses the escape key or whatever button he is suppose to, to pause the game, you can record the transform of player and assign it when needed.

What i mean is rather then checking condition for whether the game is paused or not, you can check the condition when the player presses the button.

avatar image impurekind darkStar27 · Dec 10, 2018 at 10:52 AM 0
Share

As far as I'm aware that's what I am doing.

Show more comments
avatar image
0

Answer by nicholasw1816 · Dec 10, 2018 at 02:08 PM

You say you can store position but not rotation? Create a quaternion variable in your script, then store the players rotation in the quaternion. Example

 private Quaternion myRotation;
  
 void SomeFunction()
 {
     //Store your rotation
     myRotation = transform.rotation;
     //Now when you need to reapply rotation do this.
     transform.rotation = myRotation;
 }
Comment
Add comment · 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

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

100 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 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

Find Vector3 perpendicular to Vector3 A in direction of Vector3 B 1 Answer

Get gameobject limits 1 Answer

Heat-seeking missile code. 1 Answer

Good Solution for Camera Movement 4 Answers

Child object using Origin (0, 0, 0) as its forward direction 1 Answer


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