Skip to content

sync_simple_transforms only checks for removed parents and doesn't filter for Without<Parent> #9517

@ickshonpe

Description

@ickshonpe

Bevy version

Bevy main 3aad5c6

What you did

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(Update, remove_and_set_parent)
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn(Camera2dBundle::default());
    commands.spawn(SpriteBundle {
        sprite: Sprite {
            color: Color::WHITE,
            custom_size: Some(Vec2::splat(100.)),
            ..Default::default()
        },
        transform: Transform::from_translation(50. * Vec3::ONE),
        ..Default::default()
    })
    .with_children(|commands| {
        commands.spawn(SpriteBundle { 
            sprite: Sprite {
                color: Color::RED,
                custom_size: Some(Vec2::splat(110.)),
                ..Default::default()
            },
            transform: Transform::from_translation(50. * Vec3::ONE),
            ..Default::default()
        });
    });
}

fn remove_and_set_parent(
    input: Res<Input<KeyCode>>,
    mut commands: Commands,
    query: Query<(Entity, &Parent), With<Sprite>>,
) { 
    if input.just_pressed(KeyCode::Space) {
        for (e, p) in query.iter() {
            commands.entity(e).remove_parent();
            commands.entity(e).set_parent(p.get());
        }
    }
}

run it and you get:

transform_bug_1

after pressing space:

transform_bug_2

What went wrong

sync_simple_transforms only checks for removed parents and doesn't filter for Without<Parent>, so it overwrites the GlobalTransform of non-orphan entities that removed and then added a parent since the last update.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-TransformTranslations, rotations and scalesC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions