Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 · Oct 30, 2018 at 11:40 AM · transformpositionvector3direction

Why are my player's positions not being set correctly?

Here's the code I'm using to store the player's current position when I press the pause button, and I then jump him to a new position in the level (the place where I have created the 3D pause room), and return him back to the stored position when I resume the game again:

 if (gamePaused == false)
         {
             playerStoredPosition = player.transform.position; // Stores the position of player in the level
             playerStoredForwardDirection = player.transform.forward; // Stores the forward direction of player in the level
 
         player.transform.position = playerPausePosition; // Moves player to the pause position (as set in inspector)
 
         gamePaused = true;
     }
     else if (gamePaused == true)
     {
         player.transform.position = playerStoredPosition; // Returns player to stored position
         player.transform.forward = playerStoredForwardDirection; // Returns player to stored direction
 
         gamePaused = false;
     }

Or at least that's how it's supposed to work.

But the player doesn't jump to the correct place I set for the playerPausePosition in the Inspector. Where he ends up is often quite a bit off from the position I've set, and the amount it's off varies each time too. And when he returns to the playerStoredPosition he's facing in the wrong direction if I turned at all when in the pause room.

So, it's not jumping to the correct position in the pause room as set in the Inspector (it's not even getting near the pause room location most of the time), and it's not facing the player in the correct direction when I resume the game (which should be the direction he was facing when I pressed pause).

Any ideas what's up with that?

Comment
Add comment · Show 9
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 Sonky108 · Oct 30, 2018 at 12:11 PM 0
Share

It seems that something has to override your player's position. Does it have any velocity/angular velocity? Does the presented function is inside Update loop or is called from outside?

avatar image impurekind Sonky108 · Oct 30, 2018 at 12:49 PM 0
Share

It's a function that exist outside but that's called from inside Update every time the player presses the pause button. The following part is inside Update:

 // For setting if the game is paused or not
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return) || Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P) || OVRInput.GetDown(OVRInput.Button.Two))
         {
             PauseController();
         }

And then the PauseController() function that exists outside of Update contains the reset of the code:

 public void PauseController()
     {
         if (gamePaused == false)
         {
             playerStoredPosition = player.transform.position; // Stores the position of player in the level
             //playerStoredForwardDirection = player.transform.forward; // Stores the forward direction of player in the level
             playerStoredForwardDirection = player.transform.rotation; // Stores the direction of player in the level
 
             player.transform.position = playerPausePosition; // $$anonymous$$oves player to the pause position (as set in inspector)
 
             gamePaused = true;
         }
         else if (gamePaused == true)
         {
             player.transform.position = playerStoredPosition; // Returns player to stored position
             //player.transform.forward = playerStoredForwardDirection; // Returns player to stored direction
             player.transform.rotation = playerStoredForwardDirection; // Returns player to stored direction
 
             gamePaused = false;
         }
 
         // $$anonymous$$akes somes things in the game just stop moving and/or running at all
         if (Time.timeScale == 1)
         {
             Time.timeScale = 0;
         }
         else if (Time.timeScale == 0)
         {
             Time.timeScale = 1;
         }
     }

It doesn't have any velocity that I'm aware of.

Even if I press the pause button when the player is absolutely stationary and I don't press any other movement controls or anything, he still doesn't jump to the playerPausePosition position I've set for him.

avatar image Sonky108 impurekind · Oct 30, 2018 at 12:54 PM 0
Share

That's odd. $$anonymous$$y guess is: can try the code without setting the timeScale? Or set it to something different than 0. I wonder if it change something. How do you control player's movement?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by eneroth3 · Oct 30, 2018 at 01:08 PM

In my experience characters can be slightly tricky to move with code. I've been working on a teleport script, and had lots of problem with setting the rotation. It turned out the character controller script kept track of rotation internally, so I had to edit it to expose a function to re-read the rotation from the transform.

Btw, if you keep the comments on second lines so they don't wrap, the code will be much easier to read. Also you don't need to compare a boolean with true or false, you can just use the value directly in an if statement. true == true evaluates to true, making the comparison redundant.

Comment
Add comment · Show 1 · 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 · Oct 30, 2018 at 01:27 PM 0
Share

This definitely could be part of it, but I'm totally intimidated by the scripts that are already made by Unity, or whomever, and that contain code I don't really understand. And this is just for the PC version. There's also an Oculus character controller I use when in VR mode that's even more complicated looking. :-o

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

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

How to rotate an object to face the direction it's going? 1 Answer

Finding difference between gameObject's x coordinate according to one object's transform 1 Answer

What is wrong with my zipline script? 2 Answers

Transform angle to grid position 2 Answers

Stop transform position moving objects together 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