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

Add Grounded marker for KinematicCharacterController #504

Open
Jondolf opened this issue May 9, 2024 · 0 comments
Open

Add Grounded marker for KinematicCharacterController #504

Jondolf opened this issue May 9, 2024 · 0 comments
Labels
A-Integration very bevy specific D-Medium P-Medium S-not-started Work has not started

Comments

@Jondolf
Copy link

Jondolf commented May 9, 2024

A pretty common question I see people ask is "how do I know if my character is grounded?"

This is currently done by querying for the KinematicCharacterControllerOutput component and checking the is_grounded property.

fn log_grounded(controllers: Query<(Entity, &KinematicCharacterControllerOutput)>) {
    for (entity, output) in &controllers {
        println!("Entity {:?} touches the ground: {:?}", entity, output.grounded);
    }
}

However, in my opinion, a more idiomatic approach would be a Grounded marker component. Paired with the Without filter, it would make some systems more ergonomic while also reducing the amount of iteration. For example, a simple jumping system could filter out entities that aren't grounded:

fn jump(
    mut jump_event_reader: EventReader<JumpEvent>,
    mut controllers: Query<&mut Velocity, With<Grounded>>,
) {
    for event in jump_event_reader.read() {
        for (jump_impulse, mut velocity) in &mut controllers {
            // A KCC isn't controlled like this, but imagine it works for demonstration purposes.
            velocity.linvel.y = event.jump_impulse;
        }
    }
}

For the boolean value, the idiomatic approach is to use Has:

fn log_grounded(controllers: Query<(Entity, Has<Grounded>)>) {
    for (entity, is_grounded) in &controllers {
        println!("Entity {:?} touches the ground: {:?}", entity, is_grounded);
    }
}

If desired, the name could also be more specific like CharacterGrounded or even KinematicCharacterGrounded, but I think just Grounded is fine too.

@Vrixyz Vrixyz added D-Medium P-High arbitrary important item S-not-started Work has not started A-Integration very bevy specific P-Medium and removed P-High arbitrary important item labels May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Integration very bevy specific D-Medium P-Medium S-not-started Work has not started
Projects
None yet
Development

No branches or pull requests

2 participants