- Home /
Answer was found
Why the player's movement freezes when I change the game's sound effects?
Hi,everyone! The problem is: When I added on my settings menu a option to adjust the volume of the game and I played the first level,I could move my player without any problem,but when I activated the pause menu and clicked the menu option so that I could go to the main menu to change the volume again and I went back,my player couldn't move,I pressed "w" and it wasn't moving.I'll put the player movement script here:
using UnityEngine;
public class PlayerMoviment : MonoBehaviour
{ //É uma referência para "Rigidbody" o "rb"
public Rigidbody rb;
public float forwardForce = 2000f;
// Update is called once per frame
//FixedUpdate é usado para brincar com o código
void FixedUpdate()
{ //Adiciona uma força para frente
rb.AddForce(0 ,0, forwardForce * Time.deltaTime);
if (Input.GetKey("d"))
{
//É apenas executado se a condição é apresentada
rb.AddForce(40 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-40 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("w"))
{
rb.AddForce(0, 34 * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("s"))
{
rb.AddForce(0, 0, -34 * Time.deltaTime, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
Answer by fblast1 · May 06, 2020 at 01:09 PM
How does it activate the menu itself ?
Do you manipulate timescale at any point of the pause?
Regards
I created a script called:Pause$$anonymous$$enu,when I press the Escape button it activates like other game that you can resume,go to the main menu or quit the game. The problem is that: $$anonymous$$y game starts with the main menu where you can click on:Start game button(starts the first level),Settings$$anonymous$$enuButton(Go to the settings options)and QuitGameButton(Closes the game). On the settings options,there is a slider that controls the volume of the game,for now it just changes the coins' sound-effects,beacause I didn't put any background music for the game yet. The script that controls the volume option on the slider is on the;Settings$$anonymous$$enuScript. After you change the volume on the slider for the 1st time it works perfectly...
But if you press Esc button activating the pauseScreen and click on the menuButton(Goes to the main menu) and enter on the settings menu, the first thing wrong is that the volume goes back to 0 on the slider after you pressed the menubutton and went back to the mainmenu but if you c hange it again or not,click the startgamebutton and loads the nextscene,the main character can't move when you press:w;a;s;d.That's my problem that I need help!
Well, If i got it correctly , then you are not keeping the slider values consistent between scenes one way of doing so to use PlayerPrefs to save,load as needed.
for the movement part, is there any log you got in the console ?
consider this scenario start>play>pause>menu>start>play does it move then without touching the slider ?
Regards
Actually,after I read your reply to my comment,I made a test and I discovered that the problem is not the volume,but it is something else. I clicked on the Start Game button,then it loaded the level1,then I pressed Esc,clicked $$anonymous$$enu on the pause screen,went back to the main menu,then I clicked again on the start game button,loaded level1 and I couldn't move,so actually I found out that the problem is not the volume slider,but I still don't know what is causing this,I would really aprecciate if you help me,please!
And for your 2nd question:no!. There isn't any log in the console.
While the game is running try inspecting the loaded gameobjects and check if there is a script missing when loaded
Also try to put Debug.Log at some points to check if code reaches this part or not
lastly, how do u instance your player in game or do you put it in the levels scene manually?
Regards
$$anonymous$$y player is a capsule,this is my first project so I wanted to create something simple,the capsule was the first thing that I added to the project,there are 5 levels for now in my project,I just duplicated the previously levels until I reached level5 so the capsule went along with the duplication
I've put your code in action for testing purpose with simple scene loading scenario I changed a bit for rigidbody assigning
Rigidbody rb;
public float forwardForce = 2000f;
// Update is called once per frame
//FixedUpdate é usado para brincar com o código
private void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{ //Adiciona uma força para frente
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d"))
{
//É apenas executado se a condição é apresentada
rb.AddForce(40 * Time.deltaTime, 0, 0, Force$$anonymous$$ode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-40 * Time.deltaTime, 0, 0, Force$$anonymous$$ode.VelocityChange);
}
if (Input.GetKey("w"))
{
Debug.Log("w presed");
rb.AddForce(0, 34 * Time.deltaTime, 0, Force$$anonymous$$ode.VelocityChange);
}
if (Input.GetKey("s"))
{
rb.AddForce(0, 0, -34 * Time.deltaTime, Force$$anonymous$$ode.VelocityChange);
}
}
Regards
I tried your code(you actually forgot to put the monobehaviour class in here,but its okay),but it didn't resolve my problem,my player still frozes after I click on the menubutton and play again.
well , try to load between scenes without UIs by using a script and input from keyboard.
also do you mean freezing like hanged and you quit or it crashes the editor ?
if it doesnt crash the editor , check the Hierarchy while playing and look at the capsule does it have the script attached ?
and check in the console if the error messages is disabled
Regards
Follow this Question
Related Questions
Cancel Touch on Pause 1 Answer
lower volume on audio clip C# 1 Answer
Android: Ringer Volume Control 0 Answers
Is it possible to change Audio Manager Volume by script? 1 Answer
Sound volume too low on Android... 3 Answers