Bevy | Player Following Text

Hi,

I wanted to create a text that follows player and did this:

pub fn update(
        mut text_transform_query: Query<
            &mut Transform,
            (With<MyText>, Without<Player>),
        >,
        mut text_query: Query<&mut Text2d, With<MyText>>,
        player_transform_query: Query<&Transform, (With<Player>, Without<MyText>)>,
        player_query: Query<&Player, With<Player>>,
    ) {
        let mut text_transform = text_transform_query.get_single_mut().unwrap();
        let mut text = text_query.get_single_mut().unwrap();
        let player = player_query.get_single().unwrap();
        let player_transform = player_transform_query.get_single().unwrap();

        let mut new_text_position = player_transform.translation.truncate().extend(0.0);
        new_text_position += Vec3::new(50.0, 50.0, 1.0);
        text_transform.translation = new_text_position;
        *text = format!("Point = {}", player.point).into();
    }

This actually works and there is a text that actively updates itself both for position and point.

Problem is it's not optimized, it's shaking and staying behind from player a little. Especially in high speed. What is the optimized way to do it. I want a text that follows player and updates itself actively?

I know there are some folks who use Bevy on these forums so you might get a good answer from them, but generally speaking it's probably a good idea to ask these kinds of questions in a Bevy-specific forum. (I think the main official platforms they use are GitHub Discussions and Discord).

I'm very new to Bevy, so I should probably not even try to speculate here .. but what I suspect is happening is that Bevy sees that you're querying read-only access to player positions in the function you posted, and you have a different system where you write the player's positions. Because one is mutable it can't run the two systems in parallel. However, the ordering of the systems isn't defined (unless you constrain it). So sometimes your player position will update first (before the frame is redrawn), and other times your text will update first. This could cause visible jittering.

If my guess is correct, you could try to add an after() (or before()) rule to ensure that the text update is only done after the player's position is changed.

2 Likes

Thanks,

I'm also new on Bevy and after before strategy solved my problem.

Also thanks for informing about Bevy specific forums. I will remember that.