#how to spawn Missile forward nose rotated

1 messages · Page 1 of 1 (latest)

wraith helm
#

when I'm spawning missile the tip of the Missile is pointing upwards that is to the Y axis. How do I rotate the missile so that it can point towards the asteroids. on 2D alike screen in a 3D environment.

In my game X LEFT pointed, Z is 90deg upward, and Y is towards the user screen. (missile model https://poly.pizza/m/1Xid2Qhqn2s)

Here is my missile spawn code:

fn spawn_missile(
    mut commands: Commands,
    scene_assets: Res<SceneAssets>,
    query: Query<&Transform, With<Spaceship>>,
    keyboard_input: Res<ButtonInput<KeyCode>>
) {
    let transform = query.single();
    let acceleration = Vec3::new(0., 0., -MISSILE_ACCELERATION);
    let _transform = Transform::from_translation(transform.translation + -transform.forward() * MISSILE_FORWARD_SPAWN_OFFSET);
    if keyboard_input.just_pressed(KeyCode::Space) {
        commands.spawn((
            MovingObjectBundle {
                acceleration: Acceleration::new(acceleration),
                velocity: Velocity::new(-transform.forward() * MISSILE_SPEED),
                model: SceneBundle {
                    scene: scene_assets.missile.clone(),
                    transform: _transform,
                    ..default()
                }
            }
        ));
    }
}

and here is my camera:

fn spawn_camera(mut commands: Commands) {
    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(0., CAMERA_DISTANCE, 0.).looking_at(Vec3::ZERO, Vec3::Z),
        ..default()
    });
}
normal burrow
#

Looks like you're following the Zymartu games tutorial series, my favorite Bevy tutorial. Here is a snippet of code which correctly spawns the missile on my computer:

fn spaceship_weapon_controls(
    mut commands: Commands,
    query: Query<&Transform, With<Spaceship>>,
    keyboard_input: Res<Input<KeyCode>>,
    scene_assets: Res<SceneAssets>,
) {
    let Ok(transform) = query.get_single() else {
        return;
    };
    if keyboard_input.pressed(KeyCode::Space) {
        commands.spawn((
            MovingObjectBundle {
                velocity: Velocity::new(-transform.forward() * MISSILE_SPEED),
                acceleration: Acceleration::new(Vec3::ZERO),
                collider: Collider::new(MISSILE_RADIUS),
                model: SceneBundle {
                    scene: scene_assets.missiles.clone(),
                    transform: Transform::from_translation(
                        transform.translation + -transform.forward() * MISSILE_FORWARD_SPAWN_SCALAR,
                    ),
                    ..default()
                },
            },
            SpaceshipMissile,
            Health::new(MISSILE_HEALTH),
            CollisionDamage::new(MISSILE_COLLISION_DAMAGE),
        ));
    }
}
wraith helm
#

yes you are right 🙂 thanks, but this doesn't work when i rotate the spaceship. i will share my code which I found to be working.