I currently have a movement script using _physics_process(), and I'd like to switch it to _unhandled_input(), as I've heard that it's better to use. The problem is that I want my code to not run differently depending on the framerate. When using _physics_process(), I can use the delta variable to achieve this. But I can't find anything about this with unhandled_input(). Is there a way to achieve this?
#Change movement code to use unhandled_input
29 messages · Page 1 of 1 (latest)
To get deltatime; you could either do
Node.get_process_delta_time(), or
var dt
func _process(delta):
dt = delta
func foo():
movement * dt
...
I dont see why you want to avoid _physics_process() tho.
I'm in a similar boat, but the initial assumption is flawed : Some actions are discrete and some are continuous. Jumping is discrete since it only happen when you press the button, where anything related to movement is continuous since the joystick have values. Access to the physics to query raycast is available in _physics_process, so those must be done there.
fortunately, velocity can be modified at will, so my pseudo code look like this :
extend kinematicbody
var velocity = Vector3.ZERO
func physics_process(delta):
velocity = move_and_slide_with_snap(velocity) # first thing to do is movemodify the velocity for the next frame.
velocity += -global_transform.basis.y * gravity_force
func _unhandled_input(event):
if event is action (JUMP) and is_on_floor():
velocity = Plane(up_direction).project(velocity) + global_transform.basis.y * JUMPFORCE
Even if you use _unhandled_input entirely for handling your input, you would still only use delta within _physics_process. You would update some variables in _unhandled_input, then _physics_process would use those values to know what to do
would usually want to avoid using _physics_process for capturing inputs, when you want your gui to intercept inputs
eg, your clicking on item bar, and the character is moving towards the direction
(Would want to avoid that)
then for key inputs, when you have settings opened, it would be weird to still be able to control your character
Side note: To consume key inputs, you'd need to grab_focus from the popup
and yes there are workarounds, but they're very fragile and error prone
Yeah true, I thought OP wanted to completely avoid physics process.
"I currently have a movement script using _physics_process(), and I'd like to switch it to _unhandled_input()"
That's what I'm looking for, thanks!
EDIT: seems to REALLY lag my code, will probably disable movement when in a UI or something
You might've heard this from my post yesterday. LoneDespair explained it there pretty well. Basically, you can have the input function update your direction vector and then use that vector in say, a move function inside you physics process. That way input only takes care of the, well, input.
If you're trying to use delta in an input function, you're probably doing it wrong. That's probably why you're experience something that looks like lag
The input function should only handle input, not movement
This ^
So use the input func for the input of the movement and then use that data inside the process func like I explained above
The only input I use in the movement function is Input.get_axis(), and passing those args into the function still doesn't work.
Define var direction as a field. Then, in Input, do direction = Vector2(get_axis('right', 'left'), get_axis('down','up'))
It doesn't lag, but instead the movement is glitching. After changing a small bit of code(refactored the turning), now the movement is delayed(specifically the moving in the direction it's facing). I'll probably try to figure out a workaround, and looking through other 3D character controllers on the internet, none of them seem to use unhandled_input
do you mean that when you click to move, there's a noticeable delay? or something else?
also btw are you using _unhandled_input to check whether it is still pressed?
otherwise, when you press move, your character should continue to move in that direction until it unpresses
do you mean that when you click to move, there's a noticeable delay? or something else?
I don't use click to move, i'm using WASD. What I mean is even after the rotation, the cube will continue moving in the original rotation for a second
also btw are you using _unhandled_input to check whether it is still pressed?
I check if a movement input is being pressed, and if so run the movement function
-
I suppose instead of storing the direction, you can store the state of what's pressed, and then mix it with your rotation, so your character can move in the right direction
-
I thought you're using physics_process to move your character? and _unhandled_input for collecting your input?
In any case, if this solution causes more problem that in solves, then I suppose, just using physics_process for everything, might be the solution for your project
It's moving in the right direction, just slightly delayed
_physics_process was used beforehand, I tried changing it to be _unhandled_input(realized now that I went about it the wrong way though)
huh if its already moving then it shouldn't be delayed
Yeah, that's what I'm planning, though I just(while typing) thought of an idea that could work
do you mean by delay, is after you change your direction?
yeah, it continues moving in the previous direction for a sec
I tried to test which will receive the input first when I press/release a key, it shows that _unhandled_input receives it first
so not sure why it would be delayed in your case, do you mind sharing a bit of snippet of your code?
Do you have any smoothing going on?
Maybe adding/subtracting from your movement vector in places where you shouldn't?
already deleted :(. That being said, I have a workaround idea, so i'll probably try implementing that
EDIT: not sure _unhandled_input would even work in this case, since it only fires when a button is pressed, not when it's being held