Added mouse controls
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
use std::ops::Add;
|
||||
use bevy::input::mouse::MouseMotion;
|
||||
use bevy::log;
|
||||
use bevy::prelude::*; // Core Bevy types
|
||||
|
||||
const GRAVITY: f32 = -0.981;
|
||||
const CAMERA_SPEED: f32 = 20.0; // units per second
|
||||
const CAMERA_SENSITIVITY: f32 = 1.0;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(Update, (apply_gravity, velocity_timestep).chain())
|
||||
.add_systems(Update, move_camera)
|
||||
.add_systems(Update, (camera_movement_controller, camera_rotation_controller))
|
||||
.run();
|
||||
}
|
||||
|
||||
@@ -44,6 +48,8 @@ fn setup(
|
||||
Camera3d::default(),
|
||||
Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
|
||||
));
|
||||
|
||||
log::info!("Setup finished");
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
@@ -61,7 +67,7 @@ fn apply_gravity(time: Res<Time>, mut query: Query<&mut Velocity>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn move_camera(
|
||||
fn camera_movement_controller(
|
||||
time: Res<Time>,
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut camera: Single<&mut Transform, With<Camera3d>>,
|
||||
@@ -69,10 +75,10 @@ fn move_camera(
|
||||
let mut direction = Vec3::ZERO;
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||
direction.y += 1.0;
|
||||
direction.z -= 1.0;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyS) {
|
||||
direction.y -= 1.0;
|
||||
direction.z += 1.0;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||
direction.x -= 1.0;
|
||||
@@ -80,5 +86,33 @@ fn move_camera(
|
||||
if keyboard_input.pressed(KeyCode::KeyD) {
|
||||
direction.x += 1.0;
|
||||
}
|
||||
camera.translation += direction * CAMERA_SPEED * time.delta_secs(); // or use time.delta_seconds()
|
||||
if keyboard_input.pressed(KeyCode::Space) {
|
||||
direction.y += 1.0;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::ShiftLeft) {
|
||||
direction.y -= 1.0;
|
||||
}
|
||||
|
||||
let rotation = camera.rotation;
|
||||
camera.translation += rotation * direction * CAMERA_SPEED * time.delta_secs(); // or use time.delta_seconds()
|
||||
}
|
||||
|
||||
fn camera_rotation_controller(
|
||||
time: Res<Time>,
|
||||
mut mouse_motion: MessageReader<MouseMotion>,
|
||||
mut camera: Single<&mut Transform, With<Camera3d>>,
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
){
|
||||
for motion in mouse_motion.read() {
|
||||
let rotation: Vec3 = vec3(-motion.delta.y,-motion.delta.x,0.0) * time.delta_secs() * CAMERA_SENSITIVITY;
|
||||
let translation: Quat = Quat::from_scaled_axis(rotation);
|
||||
camera.rotation *= translation;
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyQ){
|
||||
camera.rotation *= Quat::from_scaled_axis(Vec3::Z * CAMERA_SENSITIVITY * time.delta_secs());
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyE){
|
||||
camera.rotation *= Quat::from_scaled_axis(Vec3::NEG_Z * CAMERA_SENSITIVITY * time.delta_secs());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user