Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_third_person_camera"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
description = "A third person camera written for Bevy"
keywords = ["bevy", "camera", "orbit", "gamedev"]
Expand All @@ -14,8 +14,8 @@ exclude = ["assets/*", "migrationGuides/*"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = { version = "0.16", default-features = false, features = ["bevy_window"] }
bevy = { version = "0.17.3", default-features = false, features = ["bevy_window"] }

[dev-dependencies]
bevy = "0.16"
bevy_rapier3d = "0.29"
bevy = "0.17.3"
bevy_rapier3d = "0.32.0"
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ aim_zoom: 0.7, // default
aim_button: Some(MouseButton::Right), // default
zoom: Zoom::new(1.5, 3.0)
```

![aim demo](assets/aimDemo.gif)

### Cursor Lock
Expand All @@ -101,6 +102,7 @@ cursor_lock_toggle_enabled: true,
cursor_lock_active: true,
cursor_lock_key: KeyCode::Space,
```

![cursor lock demo](assets/cursorLockDemo.gif)

### Orbit
Expand Down Expand Up @@ -158,7 +160,7 @@ When using third party physics engines such as bevy rapier 3d or avian 3d, you s
## Default Controls

| Action | Mouse/Keyboard | Gamepad | Enabled by Default |
| ------------------ | ------------------- | ------------ | ------------------ |
|--------------------|---------------------|--------------|--------------------|
| Zoom In | Scroll Up | D Pad Up | Yes |
| Zoom Out | Scroll Down | D Pad Down | Yes |
| Aim | Right Mouse Button | Left Trigger | No |
Expand All @@ -169,7 +171,8 @@ When using third party physics engines such as bevy rapier 3d or avian 3d, you s
## Bevy Version Compatibility

| bevy | bevy_third_person_camera |
| ---- | ------------------------ |
|------|--------------------------|
| 0.17 | 0.4.0 |
| 0.16 | 0.2.1 - 0.3 |
| 0.15 | 0.2.0 |
| 0.14 | 0.1.11 - 0.1.14 |
Expand Down
1 change: 0 additions & 1 deletion examples/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ fn spawn_camera(mut commands: Commands) {
fn spawn_world(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,

mut materials: ResMut<Assets<StandardMaterial>>,
) {
let floor = (
Expand Down
9 changes: 1 addition & 8 deletions examples/physics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// NOTE: This example does not work at the moment. Waiting for rapier to update to latest bevy version.

/*
Example displaying the integration with a third party physics engine. In this case Bevy Rapier 3d.
The key is to run the CameraSyncSet AFTER the PhysicsSet, see line 19.
Expand Down Expand Up @@ -93,14 +91,9 @@ fn player_movement_keyboard(
time: Res<Time>,
keys: Res<ButtonInput<KeyCode>>,
mut player_q: Query<(&mut ExternalImpulse, &mut Transform, &Speed), With<Player>>,
cam_q: Query<&Transform, (With<Camera3d>, Without<Player>)>,
cam: Single<&Transform, (With<Camera3d>, Without<Player>)>,
) {
for (mut ext_impulse, mut player_transform, player_speed) in player_q.iter_mut() {
let cam = match cam_q.single() {
Ok(c) => c,
Err(e) => Err(format!("Error retrieving camera: {}", e)).unwrap(),
};

let mut direction = Vec3::ZERO;

// forward
Expand Down
59 changes: 23 additions & 36 deletions src/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,66 +33,53 @@ fn detect_gamepad(mut gamepad_connected: ResMut<GamepadConnected>, gamepad_q: Qu

pub fn zoom_gamepad(
btns: Query<&Gamepad>,
mut cam_q: Query<&mut ThirdPersonCamera, With<ThirdPersonCamera>>,
mut cam: Single<&mut ThirdPersonCamera, With<ThirdPersonCamera>>,
) {
if let Ok(mut cam) = cam_q.single_mut() {
let gp = &cam.gamepad_settings;

let zoom_out = gp.zoom_out_button;
let zoom_in = gp.zoom_in_button;

let mut new_radius = cam.zoom.radius;

// zoom out
for btns in btns.iter() {
if btns.pressed(zoom_out) {
new_radius += cam.zoom.radius * 0.01;
cam.zoom.radius = new_radius.clamp(cam.zoom.min, cam.zoom.max);
// zoom in
} else if btns.pressed(zoom_in) {
new_radius -= cam.zoom.radius * 0.01;
cam.zoom.radius = new_radius.clamp(cam.zoom.min, cam.zoom.max);
}
let gp = &cam.gamepad_settings;

let zoom_out = gp.zoom_out_button;
let zoom_in = gp.zoom_in_button;

let mut new_radius = cam.zoom.radius;

// zoom out
for btns in btns.iter() {
if btns.pressed(zoom_out) {
new_radius += cam.zoom.radius * 0.01;
cam.zoom.radius = new_radius.clamp(cam.zoom.min, cam.zoom.max);
// zoom in
} else if btns.pressed(zoom_in) {
new_radius -= cam.zoom.radius * 0.01;
cam.zoom.radius = new_radius.clamp(cam.zoom.min, cam.zoom.max);
}
}
}

pub fn orbit_gamepad(
window_q: Query<&Window, With<PrimaryWindow>>,
window: Single<&Window, With<PrimaryWindow>>,
mut cam_q: Query<(&ThirdPersonCamera, &mut Transform), With<ThirdPersonCamera>>,
gamepad_q: Query<&Gamepad>,
gamepad: Single<&Gamepad>,
) {
let Ok((cam, mut cam_transform)) = cam_q.single_mut() else {
return;
};

let Ok(gamepad) = gamepad_q.single() else {
return;
};

if cam.mouse_orbit_button_enabled && !gamepad.pressed(cam.gamepad_settings.mouse_orbit_button) {
return;
}

let x_axis = gamepad.right_stick().x;
let y_axis = gamepad.right_stick().y;
let x = gamepad.right_stick().x;
let y = gamepad.right_stick().y;

let deadzone = 0.5;
let mut rotation = Vec2::ZERO;
let (x, y) = (x_axis, y_axis);
if x.abs() > deadzone || y.abs() > deadzone {
rotation = Vec2::new(x, y);
}

if rotation.length_squared() > 0.0 {
let window = window_q.single().unwrap();
let delta_x = {
let delta = rotation.x / window.width()
* std::f32::consts::PI
* 2.0
* cam.gamepad_settings.sensitivity.x;
delta
};
let delta_x = rotation.x / window.width() * PI * 2.0 * cam.gamepad_settings.sensitivity.x;

let delta_y = -rotation.y / window.height() * PI * cam.gamepad_settings.sensitivity.y;
let yaw = Quat::from_rotation_y(-delta_x);
let pitch = Quat::from_rotation_x(-delta_y);
Expand Down
Loading