Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keyboard_input.just_pressed in basic_dynamic_character example works only sometimes #170

Closed
silentbat opened this issue Oct 8, 2023 · 1 comment · Fixed by #210
Closed
Labels
C-Examples Improvements or additions to examples

Comments

@silentbat
Copy link

Very new to bevy_xpbd, I hope this is not a silly question..

What I did:
run the "basic_dynamic_character" example in release mode

cargo run --example basic_dynamic_character --release

and then press space/jump multiple times.

What I expected to happen:
the capsule/character should jump every time I press space, as long as it touches the ground when pressing.

What happened:
the jump happens only sometimes, seems random at first.

I think the problem is that the system responsible for keyboard input handling is running on the physics schedule and therefore .just_pressed() is not working as expected (keys are not just pressed anymore in most cases but only pressed).

All other buttons are working fine, as well as the grounded condition. Also: when changing the line to
if keyboard_input.pressed(KeyCode::Space) && !ground_hits.is_empty() {
the jumping works as intended.

Is it just me that I am not familiar enough with bevy_xpbd to know what I should do in such cases or should the example be "fixed"?

@stargazing-dino
Copy link

stargazing-dino commented Oct 9, 2023

Yeah, I noticed the same behavior in my own game which I resolved by placing my jump code in an Update schedule. I think this is a bevy issue though and not a xpbd issue: bevyengine/bevy#6183

I think one workaround could be to separate out the input handling logic into an update schedule and have that just send events. You could then consume those events in the actual movement code similar to what's recommended here

@Jondolf Jondolf added the C-Examples Improvements or additions to examples label Oct 9, 2023
Jondolf added a commit that referenced this issue Oct 29, 2023
# Objective

Fixes #170.

Currently, the movement in examples like `move_marbles` and `cubes` is frame rate dependent because they are running in `Update` but don't use delta time. `basic_dynamic_character` and `basic_kinematic_character` on the other hand run in the `PhysicsSchedule`, but this can cause issues with missed inputs as reported in #170.

The character controllers are also perhaps too basic and don't reflect real usage well, and the movement code isn't great and it doesn't support WASD.

## Solution

- Put all movement systems in `Update` and use delta time (alternatively, you could send input events in `Update` and have another system handle the movement in the `PhysicsSchedule`)
- Refactor input handling to be cleaner and to support WASD as well as arrow keys
- Refactor character controller examples with `CharacterControllerBundle` and configuration options
- Fix some meshes not being rendered due to `clone_weak`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Examples Improvements or additions to examples
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants