However the player always jumps a very small amount before everything is paused. Upon clicking a “resume” button, the player continues it’s upwards trajectory.
Is there a way to make it instantaneous? I’ve tried creating boolean !paused flags, changing script execution orders and even using old OnGUI button without success. I think this slight delay is causing it to register the mousedown before time stopping.
My intended game consists of simple tap left or right screen inputs, so the only solution which I’ve tried with success is creating two invisible UI buttons filling up each half of screen, and using the button’s OnClick component to execute the jump. This method allows the pause to work.
But is there a better way though? I’m a bit uncertain this is the “correct” solution.
time.deltaTime is calculated at the beginning of each frame before any scripts are run. Even after you set Time.timeScale to 0, you still have your current frame’s worth of movement. You are setting your player’s velocity the same frame, which gives it the 1 frame to move before the game is fully paused at the beginning of the next frame.
No guarantees, but if you put your pause last in the script execution order, that may work better.
I can’t recall the default settings, but you might want to check your inputs’ gravity settings. These can really make your input feel very sluggish and unresponsive.
I tried both putting the pause function both first and last on the execution order, but both ways still doesn’t work. But what you said got me thinking about the framerates, so I put a print statement to print out the Time.framecount at the moment of mousedown (on update()) and on the pausebutton function. So upon clicking the pause button both print statements will fire at the same time. And apparently the pause is called a consistently 4-6 frames after mousedown. And this is irregardless of whether the pause script is before or after my inputmanager script in the execution order.
I have come across this exact same problem just this morning. The specifics of my situation are slightly different than yours, but the problem is the same.
There appears to be a n-frame delay from when MouseDown is detected vs when the button calls its registered OnClick callback. As far as I’m concerned, this is broken.
I understand that input is asynchronous and most engines queue it up. However, that has nothing to do with the button taking n-frames to invoke its OnClick callback. Why is OnClick not called the frame that MouseDown is detected within that button?
I’m assuming you mean using the event trigger PointerEnter component? Sounds feasible but ultimately I’m working on a mobile game. How would I go about this for touch controls?
Bad wording on my part. The new UI system abstracted away the concept of a touch and a mouse into a single concept of a pointer. So you can check if the pointer is over a UI element.
I mention several ways to deal with unwanted click propagation in this video.