- Home /
After resuming the game - wrong position of player
Hi,i have a bug in my infinite runner/platformer 2d game.I have a code which moves the camera to the right,this is forcing the player to stay within the bounds of the camera.Here is the script for it:
public class CameraRunnerScript : MonoBehaviour {
public Transform player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.position = new Vector3 (transform.position.x + 0.1f, 0, -10);
Now the problem is after pausing and resuming the game.Here is the code for that in another script:
public class PauseGameMenu : MonoBehaviour {
public bool isPaused;
public GameObject pauseMenuCanvas;
// Update is called once per frame
public void Update () {
if(isPaused)
{
pauseMenuCanvas.SetActive(true);
Time.timeScale = 0f;
//transform.position =GameObject.FindGameObjectWithTag("MainCamera").transform.position(0,0,0) ;
}
else
{
pauseMenuCanvas.SetActive(false);
Time.timeScale = 1f;
}
if (Input.GetKeyDown (KeyCode.Escape))
{
isPaused = !isPaused;
}
}
public void Resume ()
{
isPaused = false;
pauseMenuCanvas.SetActive(false);
Time.timeScale = 1f;
}
I commented in this script my unsuccessful attempt to get the position of the main camera and reseted after resuming the game(actually i want the game to resume with the exact same position of the player and the platforms like before pausing the game).Of course there was some error like:
'UnityEngine.Transform' does not contain definition for position.
After that i tried to add the next lines in the first code(CameraRunnerScript) in Update():
if(Time.timeScale == 0)
{
transform.position = new Vector3 (0 , 0, -10);
}
After resuming the game the player appears on some platform from the right side of the screen(outside of camera view) after a few seconds.I tried replacing the first 0(x position) with "transform.position.x" but then the while pausing, the player and the platforms kept "moving" to the left(actually the camera kept moving to the right as in playmode).So how do i resume the game from the same position as before pausing it?
Answer by game_crusher · Jun 25, 2015 at 10:29 PM
OK, i figured this one on my own.I added one line of code in the Update() function of the CameraRunnerScript.I counteracted the effect of moving the camera to the right in the playmode which is done with this line of code:
transform.position = new Vector3 (transform.position.x + 0.15f, 0, -10);
with its reverse(moving the camera to the left with the same speed) in the pause mode - all done in Update():
if(Time.timeScale == 0)
{
transform.position = new Vector3 (transform.position.x-0.15f , 0, -10);
}
I doubt this is the best solution for the CPU,so if anybody has a better solution i'm opened to suggestions
Your answer
Follow this Question
Related Questions
Vector3.MoveTowards doesnt work 1 Answer
Instantiated object not showing in scene or hierarchy 2 Answers
re-entering on the opposite side? 1 Answer
Unity at fedora linux,Unity at fedora. 0 Answers