- Home /
How to prevent character from jumping, when clicking pause-button
Hey there,
I got a little issue again: I created a Pause-Button which stops the game through changing the Time.timescale, but by clicking the button, my character jumps (jumping is forced by Input.GetButtonDown ("Fire1")), and when clicking the resume-button the game, he does his doublejump. So for that reason, I need to somehow "deactivate" the Input of the active game while hovering over or by clicking the button. The game is supposed to run on mobile phones also, so it needs to know that over the buttons, the player controls are deactivated...I found some suggestions but none of them really work for me somehow :( (like for example: http://forum.unity3d.com/threads/4-6-how-to-detect-if-any-gui-element-has-been-clicked-on.263473/)
Thanks in advance
best regards
For when the pause menu is up, I can control the playercontrols via a bool which prevents him from jumping until i clicked resume. But the true problem is to "define" the area of the pause button so that in this area the input does only count for the UI...
The solution melange brought up in your posted link sounds promising to me. What's wrong with the post?
If I got it right, it just checks if the pointer is over the button - basically, yes, that's exactly what I'm looking for, but with touch, I don't have a cursor...or am I getting something wrong??
Around your Input.GetButtonDown("Fire1") function add
if(NotPaused()) { Input.GetButtonDown("Fire1") {
} }
Create a function that represents boolean paused State
Public bool NotPaused() { //SomeCodeThatWillrepresentThePausedState;
return false; }
Another way to approach this is to build a state stack class that holds an enum with the defined states. Then whenever you enter a state just push it to the stack and have the checking code like dr3th mentioned above. You'll want the stack class to basically be a non-destroyed singleton. This solution is a little more extendable compared to a single boolean. Just some pseudocode:
public enum GameState {
InGame,
Paused
}
public class GameState$$anonymous$$anager {
private Stack<GameState> _gameStack;
// This is simplified for example, but this would have
// singleton code as well
void GameState$$anonymous$$anager() {
_gameStack = new Stack<GameState>();
}
public GameState GetGameState() {
return _gameStack.Peek();
}
public void PushState(GameState state) {
_gameStack.Push(state);
}
public void PopState() {
_gameStack.Pop();
}
}
public class $$anonymous$$yInputClass {
private GameState$$anonymous$$anager _stack;
void Start() {
_stack = GameObjects.Find("GameStackObject").GetComponent<GameState$$anonymous$$anager>();
_stack.PushState(GameState.InGame);
}
void Update() {
switch(_stack.GetGameState()) {
case GameState.InGame:
//Example purposes
if(Input.GetButtonDown("Fire1") {
Jump();
}
if(Input.GetButtonDown("PauseButton") {
_stack.PushState(GameState.Paused);
}
break;
case GameState.Paused:
DoPauseStuff();
if(Input.GetButtonDown("PauseButton") {
_stack.Pop();
}
break;
}
}
Again this is all written without a compiler so part of it is pseudocoded based on memory, my apologies if parts aren't syntactically correct.
Answer by ParanoidSnail · Mar 25, 2015 at 09:21 AM
The way dealt with mouse over ui, i have an array, that contains my UI elements that i do not want to let the click go through; I assigned the UI elements in the inspector.
//Holds all ui elements that don't let the touches/clicks pass through to the scene
public RectTransform[] uiElements;
/**
* Tests if the mouse button is over UI
*/
public bool isMouseOverUI() {
Vector2 mousePosition = Input.mousePosition;
foreach (RectTransform elem in uiElements) {
if (!elem.gameObject.activeSelf) {
continue;
}
Vector3[] worldCorners = new Vector3[4];
elem.GetWorldCorners(worldCorners);
if(mousePosition.x >= worldCorners[0].x && mousePosition.x < worldCorners[2].x
&& mousePosition.y >= worldCorners[0].y && mousePosition.y < worldCorners[2].y) {
return true;
}
}
return false;
}
When jumping, just test first for isMouseOverUI();
Alternatively you can play around with
EventSystem.current.IsPointerOverGameObject()
http://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html But i found that it did not work correctly with touches (on android device). This was in 4.6 beta, haven't tried to use it again yet, maybe it was fixed.