- Home /
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?
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?
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.
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?
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.
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