Home > OS >  Player movement doesn't work after reloading the scene in Unity
Player movement doesn't work after reloading the scene in Unity

Time:01-30

Using Unity 2021.3.16f1.

I followed this tutorial from Brackeys to make the pause menu for my flappybird like game, my player movement stops working after I exit the main game to the main menu and return to the game.

Part of player code responsible for movement:

public class birdScript : MonoBehaviour
{
    public Rigidbody2D myRigidbody;
    public float flapStrength;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            myRigidbody.velocity = Vector2.up * flapStrength;
            FindObjectOfType<AudioManager>().Play("jump");
        }

    }
}

Part of pause menu code responsible for loading the main menu scene:

using UnityEngine.SceneManagement;

public class pauseMenu : MonoBehaviour
{
    public static bool gameIsPaused = false;
    public GameObject pauseMenuUI;

    public void loadMenu()
    {
        Time.timeScale = 1f;
        SceneManager.LoadScene("Title");
    }
}

Part of code responsible for loading the main game scene:

using UnityEngine.SceneManagement;

public class playButton : MonoBehaviour
{
    public void loadLevel()
    {
        SceneManager.LoadScene("Main game");
    }
}

I tried changing the play button code that loads the main game, in the scene hierarchy my main menu scene is 0 and my main game scene is 1. The play button is on the main menu.

I turned this

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex   1);

Into this

SceneManager.LoadScene("Main game");

But nothing happened, I tried searching on google but I have no idea how to search for the right answers.

CodePudding user response:

Your problem has nothing to do with scene loading. That said your code for loading level is fine as it was:

using UnityEngine.SceneManagement;

public class playButton : MonoBehaviour
{
    public void loadLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex   1);
    }
}

Issue could be in your pauseMenu script but you only provided a part of the code. You need to make sure your Time.timeScale is set to 1f when the level scene is loaded.

I would suggest setting the default value for flapStrength. Also in void Start there is Time.timeScale set to 1f just to be sure. You could do this in the pauseMenu script.

public class birdScript : MonoBehaviour
{
    public Rigidbody2D myRigidbody;
    public float flapStrength = 10f;

    void Start()
    {
        Time.timeScale = 1f;
    }
    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            myRigidbody.velocity = Vector2.up * flapStrength;
            FindObjectOfType<AudioManager>().Play("jump");
        }
    }
}

I hope the answer helped. The issue is that the problem could be anywhere. It is necessary to go through all the values and examine the behavior of the player. Didn't an error appear that would stop the game? If the problem persists, you would need to edit the question and add more information.

  • Related