Weapon sway rot and translate when moving cursor

This commit is contained in:
Franklin 2023-11-23 13:52:19 -04:00
parent c5c8a4ba5b
commit 2d12f5b392
7 changed files with 72 additions and 33 deletions

View File

@ -21,7 +21,7 @@ Multiplayer
- [ ] Weapon Sway PENDING!!!
- [x] Fixing leaning
- [x] Snap back leaning too quick
- [ ] Issue with moving around quickly
- [x] Issue with moving around quickly
- [ ] Bring Crouching back
- [ ] Inspect animation (procedural)
- [ ] Reload animation (procedural)

Binary file not shown.

Binary file not shown.

View File

@ -3,7 +3,7 @@ use bevy_rapier3d::prelude::*;
use crate::{comps::core::markers::{proxy::physics::rapier::LinkToPlayer, camera::MainCamera}, logic::core::player::player_values_state::PlayerValuesState};
use super::third_person_camera::{ThirdPersonCameraProxy, ThirdPersonCamera};
use super::{third_person_camera::{ThirdPersonCameraProxy, ThirdPersonCamera}, in_player_hands_parent::InPlayerHandsParent};
#[derive(Component, Reflect, Default, Debug)]
@ -15,6 +15,7 @@ pub fn insert_components_into_spawned_player(
eye_query: Query<Entity, Added<PlayerEye>>,
third_person_camera_query: Query<Entity, Added<ThirdPersonCameraProxy>>,
player_collider_query: Query<Entity, (With<LinkToPlayer>, With<Collider>, Added<Collider>)>,
mut hand_query: Query<(Entity, &mut Transform), (With<InPlayerHandsParent>, Without<LinkToPlayer>, Without<Collider>)>,
player_values_state: Res<PlayerValuesState>,
) {
for eye in eye_query.iter() {
@ -22,7 +23,7 @@ pub fn insert_components_into_spawned_player(
let camera = commands
.spawn(MainCamera)
.insert(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..Default::default()
})
//s.insert(Skybox(skybox_handle.clone()))
@ -32,6 +33,10 @@ pub fn insert_components_into_spawned_player(
})
//.push_children(&[player_hand])
.id();
for (hand, mut hand_transform) in hand_query.iter_mut() {
hand_transform.rotate_y(180.0f32.to_radians());
commands.entity(hand).set_parent(camera);
}
commands.entity(eye).add_child(camera);
}
for third_person_camera in third_person_camera_query.iter() {

View File

@ -111,7 +111,7 @@ impl Firearm {
},
final_aimed_rotation: Quat::default(),
rotation_offset: Quat::default(),
final_aimed_position: Vec3 { x: 0.016, y: 0.0, z: -0.1 },
final_aimed_position: Vec3 { x: 0.025, y: 0.003, z: -0.1 },
translation_offset: Vec3 { x: -0.2, y: -0.03, z: 0.0 },
scale_factor: 0.2,
firearm_type: FirearmType::Primary,

View File

@ -1,6 +1,6 @@
use std::time::Duration;
use bevy::prelude::*;
use bevy::{prelude::*, render::render_resource::PrimitiveTopology};
use bevy_rapier3d::prelude::*;
use crate::comps::core::{markers::{bullet::BulletMarker, muzzle_flash::MuzzleFlashMarker}, weapons::caliber::Caliber};
@ -37,7 +37,7 @@ pub fn shoot_bullet(
MuzzleFlashMarker(Timer::new(Duration::from_millis(10), TimerMode::Once)),
));
// Spawn Line
/*commands.spawn(
commands.spawn(
MaterialMeshBundle {
mesh: {
let mut mesh = Mesh::new(PrimitiveTopology::LineStrip);
@ -52,7 +52,7 @@ pub fn shoot_bullet(
transform: firing_point,
..Default::default()
}
);*/
);
spawn_bullet(
commands,

View File

@ -1,8 +1,8 @@
use bevy::{input::mouse::MouseMotion, prelude::*, window::CursorGrabMode};
use bevy::{input::mouse::MouseMotion, prelude::*, window::CursorGrabMode, ecs::system::SystemParam};
//use bevy_rapier3d::prelude::*;
use crate::{
comps::core::markers::{camera::MainCamera, player::Player, proxy::{physics::utils::F32Ext, character::player_character::PlayerCharacter}},
comps::core::markers::{camera::MainCamera, player::Player, proxy::character::{player_character::PlayerCharacter, player_eye::PlayerEye}, holdable::InPlayerHands},
ui::game::game_ui_state::{GameUiState, GameUiWindow},
};
@ -29,8 +29,8 @@ impl Default for MouseMovementSettings {
/// Synchronizes camera's translation to player.
pub fn update_camera_vertical_position(
mut player: Query<(&mut Transform, &PlayerLinearXZState), (With<Player>, Without<MainCamera>)>,
mut camera: Query<&mut Transform, (With<MainCamera>, Without<Player>)>,
mut player: Query<(&mut Transform, &PlayerLinearXZState), (With<Player>, Without<PlayerEye>)>,
mut camera: Query<&mut Transform, (With<PlayerEye>, Without<Player>)>,
time: Res<Time>,
player_values_state: Res<PlayerValuesState>,
) {
@ -65,21 +65,29 @@ pub fn update_camera_vertical_position(
}
}
#[derive(SystemParam)]
pub struct FollowCursorWithCameraQueryParams<'w, 's> {
primary_window: Query<'w, 's, &'static mut Window>,
player_query: Query<'w, 's, &'static mut Transform, (With<Player>, Without<MainCamera>)>,
camera_query: Query<'w, 's, &'static mut Transform, (With<MainCamera>, Without<Player>)>,
player_model_query: Query<'w, 's, &'static mut Transform, (With<PlayerCharacter>, Without<Player>, Without<MainCamera>)>,
in_player_hands_query: Query<'w, 's, &'static mut Transform, (With<InPlayerHands>, Without<Player>, Without<MainCamera>, Without<PlayerCharacter>)>,
//in_player_hands_parent_query: Query<'w, 's, &'static mut Transform, (With<InPlayerHandsParent>, Without<Player>, Without<MainCamera>, Without<PlayerCharacter>, Without<InPlayerHands>)>
}
/// Handles looking around if cursor is locked
pub fn follow_cursor_with_camera(
settings: Res<MouseMovementSettings>,
mut primary_window: Query<&mut Window>,
mut motions: EventReader<MouseMotion>,
mut player_query: Query<&mut Transform, (With<Player>, Without<MainCamera>)>,
mut camera_query: Query<&mut Transform, (With<MainCamera>, Without<Player>)>,
mut player_model_query: Query<&mut Transform, (With<PlayerCharacter>, Without<Player>, Without<MainCamera>)>,
keyboard_input: Res<Input<KeyCode>>,
btn: Res<Input<MouseButton>>,
time: Res<Time>,
mut game_ui_state: ResMut<GameUiState>,
player_values_state: Res<PlayerValuesState>,
mut query: FollowCursorWithCameraQueryParams,
) {
if let Ok(mut window) = primary_window.get_single_mut() {
if let Ok(mut window) = query.primary_window.get_single_mut() {
if keyboard_input.just_pressed(KeyCode::Escape) {
if game_ui_state.any_window() {
// Hide any UI screen & Capture Cursor
@ -122,36 +130,61 @@ pub fn follow_cursor_with_camera(
}
if window.cursor.grab_mode != CursorGrabMode::None {
for mut player_transform in player_query.iter_mut() {
for mut camera_transform in camera_query.iter_mut() {
for mut player_model_transform in player_model_query.iter_mut() {
for mut player_transform in query.player_query.iter_mut() {
for mut camera_transform in query.camera_query.iter_mut() {
for mut player_model_transform in query.player_model_query.iter_mut() {
let (mut yaw, mut pitch) = (player_transform.rotation.to_euler(EulerRot::YXZ).0, camera_transform.rotation.to_euler(EulerRot::YXZ).1) ;
for motion in motions.read() {
let window_scale = window.height().min(window.width());
let mut weapon_sway_vec = Vec3::ZERO;
#[allow(unused_assignments)]
let mut weapon_sway_vec_rot = Vec3::ZERO;
if btn.pressed(MouseButton::Right) {
pitch -= ((settings.aimed_sensitivity
let mouse_x = (settings.aimed_sensitivity
* motion.delta.y as f64
* window_scale as f64) as f32)
.to_radians();
yaw -= ((settings.aimed_sensitivity
* window_scale as f64) as f32 ;
let mouse_y = (settings.aimed_sensitivity
* motion.delta.x as f64
* window_scale as f64) as f32)
* window_scale as f64) as f32;
pitch -= mouse_x
.to_radians();
yaw -= mouse_y
.to_radians();
weapon_sway_vec_rot = Vec3 { x: mouse_x, y: mouse_y, z: 0.0 } * -0.1;
weapon_sway_vec_rot.x = weapon_sway_vec_rot.x.clamp(-0.5, 0.5);
weapon_sway_vec_rot.y = weapon_sway_vec_rot.y.clamp(-0.5, 0.5);
} else {
pitch -= ((settings.sensitivity
let mouse_x = (settings.sensitivity
* motion.delta.y as f64
* window_scale as f64) as f32)
.to_radians();
yaw -=
((settings.sensitivity * motion.delta.x as f64 * window_scale as f64)
as f32)
.to_radians();
* window_scale as f64) as f32 ;
let mouse_y = (settings.sensitivity
* motion.delta.x as f64
* window_scale as f64) as f32;
pitch -= mouse_x.to_radians();
yaw -= mouse_y.to_radians();
// Weapon Sway Translation
weapon_sway_vec = Vec3 { x: mouse_y, y: mouse_x, z: 0.0 } * -0.01;
weapon_sway_vec.x = weapon_sway_vec.x.clamp(-0.06, 0.06);
weapon_sway_vec.y = weapon_sway_vec.y.clamp(-0.06, 0.06);
// Weapon Sway Rotation
weapon_sway_vec_rot = Vec3 { x: mouse_x, y: mouse_y, z: 0.0 } * -0.1;
weapon_sway_vec_rot.x = weapon_sway_vec_rot.x.clamp(-0.5, 0.5);
weapon_sway_vec_rot.y = weapon_sway_vec_rot.y.clamp(-0.5, 0.5);
}
// Weapon Sway
for mut in_player_hands_transform in query.in_player_hands_query.iter_mut() {
in_player_hands_transform.translation = in_player_hands_transform.translation.lerp(weapon_sway_vec, time.delta_seconds() * 1.0);
in_player_hands_transform.rotation = in_player_hands_transform.rotation.lerp(Quat::from_euler(EulerRot::XYZ, weapon_sway_vec_rot.x, weapon_sway_vec_rot.y, weapon_sway_vec_rot.y), time.delta_seconds() * 3.0);
}
}
pitch = pitch.clamp(-1.54, 1.54);
let desired_rotation_quat_camera =
Quat::from_axis_angle(Vec3::X, pitch);
let desired_rotation_quat_camera = Quat::from_axis_angle(Vec3::X, pitch);
//let desired_rotation_quat_hand = Quat::from_axis_angle(Vec3::X, pitch * -1.0);
let desired_rotation_quat_player = Quat::from_axis_angle(Vec3::Y, yaw);
@ -159,6 +192,7 @@ pub fn follow_cursor_with_camera(
//let right = Vec3::new(local_z.z, camera_transform.translation.y, -local_z.x);
camera_transform.rotation = desired_rotation_quat_camera;
player_transform.rotation = desired_rotation_quat_player;
let player_model_default_rot = Quat::from_euler(EulerRot::XYZ, -180.0f32.to_radians(), 0.0, -180.0f32.to_radians());
if keyboard_input.pressed(KeyCode::Q) {
let player_model_final_rot = Quat::from_euler(EulerRot::XYZ, -180.0f32.to_radians(), 0.0, (-180.0 - player_values_state.player_lean_angle).to_radians());