#Do components share transforms?

9 messages · Page 1 of 1 (latest)

flat sky
#

New to rust and bevy and I was trying to make a basic entity of Player move up. I gave the player a Transform component and I also gave the player a MaterialMesh2dBundle component for rendering a basic 2D circle. I thought that I would have to update the transform of the MaterialMesh2dBundle to have the same translation of the player's transform, but it seems like the circle is already updating its position without me telling it to, only the other transform component the player has. Can someone pls explain what is happening here and why it is happening?

struct Player;

#[derive(Component)]
struct Name(String);

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    commands.spawn(Camera2dBundle::default());

    let shape = Mesh2dHandle(meshes.add(Circle { radius: 25.0 }));
    let color = Color::hsl(360., 0.95, 0.7);

    let mut player = commands.spawn(Player);
    let mut player_transform = Transform::from_xyz(1000., 0., 0.);

    let player_mesh = MaterialMesh2dBundle {
        mesh: shape,
        transform: Transform::from_xyz(0., 0., 0.),
        material: materials.add(color),
        ..default()
    };

    player.insert(Name(String::from("Player")));
    player.insert(player_transform);
    player.insert(player_mesh);

}

fn update_player(time: Res<Time>, mut query: Query<(&mut Transform, &Name), With<Player>>) {

    let speed:f32 = 50.0;

    for mut q in &mut query {
        let mut pos = &mut q.0;
        pos.translation.y += speed * time.delta_seconds();;
    }
}```
graceful halo
#

An entity can only have one of each component type, so there is only one transform on your entity

flat sky
#

oh so the mesh transform just overrides the player transform?

graceful halo
#

Yep exactly

flat sky
#

hm okay is there any way around this? like if I wanted to store multiple positions on an entity?

graceful halo
#

I'm guessing you just want the player mesh to be offset from the player position? In which case just add the mesh as a child entity instead

flat sky
#

ooooh

#

I was gonna say like in godot you can have a player with multiple sprites each with their own transforms as children

#

i didnt know bevy had children and stuff lol