- Home /
c# one click and pauses the game and makes player jump
my player jumps on every click that happens . i've got a pause button(gui button) . my problem is that when i click the pause button , my player jumps once , and then the game pauses .
for player movement i've got this: if ((Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) && isPaused == false) { //jump }
and for pausing the game i've got this: if (GUI.Button (new Rect (10, 10, 100, 100), icon, myStyle)) { Time.timeScale = 0; playerMovement.Instance.isPaused = true; }
any ideas ?
Answer by Marcelo Colombo · Oct 29, 2014 at 08:36 PM
The issue relies on TIME, it detects the jump (action input) first, and later detects the pause (pause input) on the UI element (pause button).. I fix this by checking a boolean variable before triggering the jump (action input) and on the pause script, delaying by a fraction of a second when this variable is re enabled... this is how it works for me:
Action Script:
public class PlayerController : MonoBehaviour {
static public bool pauseCheck=true;
void Update()
{
if(Input.GetMouseButtonDown(0) pauseCheck ==true)
{
jump = true;
}
}
}
and Pause Script:
public class Pause : MonoBehaviour {
static public bool paused = false;
void OnMouseDown() {
paused = !paused;
PlayerController .pauseCheck =false ;
if(paused){
Time.timeScale = 0;
}
else{
Time.timeScale = 1;
StartCoroutine (pause());
}
}
IEnumerator pause()
{
yield return new WaitForSeconds(0.1f);
PlayerController .pauseCheck =true;
}
}
Hope this help.
Answer by pmurph03 · Mar 17, 2014 at 04:46 AM
Just making sure you're checking for the pause button click before the player movement. Otherwise it would jump first, and then pause, like what's happening.
Answer by TakisBeskos · Mar 17, 2014 at 10:42 AM
they are on different scripts . on different gameobjects . how to do that ?
EDIT: i've placed them on the same script , but the button is on void onGUI and movement is void update , it still jumps once before it pause the game
Your answer
![](https://koobas.hobune.stream/wayback/20220613140429im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Pause Menu problem 0 Answers
Invoke Function by Ui Button 1 Answer
Pause movement on collision 2 Answers