#Enhanced Input Mouse Look
1 messages · Page 1 of 1 (latest)
I'll paste the whole func here
fn look(
e: On<Fire<Look>>,
mut players: Query<&mut Transform, With<PlayerAvatar>>,
mut cam_pivot_q: Query<&mut Transform, (With<CameraPivot>, Without<PlayerAvatar>)>,
cursor: Res<Cursor>,
mut shoulders: Query<&mut hands::ShoulderSway>,
mut hands: Query<&mut hands::HandSway>,
) {
// if the cursor is visible, do not look:
if cursor.visible {
return;
}
let Ok(mut transform) = players.get_mut(e.context) else { return };
let Ok(mut cam_transform) = cam_pivot_q.single_mut() else { return };
let delta = e.value * 0.01;
if delta != Vec2::ZERO {
let (yaw, _, _) = transform.rotation.to_euler(EulerRot::YXZ);
let (_, pitch, roll) = cam_transform.rotation.to_euler(EulerRot::YXZ);
let yaw = yaw - delta.x;
const PITCH_LIMIT: f32 = FRAC_PI_2 - 0.01;
let pitch = (pitch - delta.y).clamp(-PITCH_LIMIT, PITCH_LIMIT);
transform.rotation = Quat::from_euler(EulerRot::YXZ, yaw, 0.0, 0.0);
cam_transform.rotation = Quat::from_euler(EulerRot::YXZ, 0.0, pitch, roll);
// pass some delta onto shoulders for sway
for mut sway in &mut shoulders {
sway.yaw.velocity += delta.x * -1.0;
}
for mut sway in &mut hands {
sway.yaw.velocity += delta.x * -1.0;
sway.pitch.velocity += delta.y * -4.0;
sway.roll.velocity += delta.x * -1.0;
}
}
}
this part worked with leafwing just like this so I'm pretty confident in it. Plus it looks like I'm doing basically the same thing as bevy_ahoy's rotate_camera() https://github.com/janhohenheim/bevy_ahoy/blob/10013c740960dd470567ca9773c26fa25dcb33b8/src/camera.rs#L174
If I print out the e.value.length() in that function and move the mouse around it outputs mostly "1" with some 0.99999s thrown in, which makes me think it's getting clamped or normalized somewhere
I just noticed: if I try the right stick on a gamepad, it rotates at a constant speed even if I barely tilt the stick, which makes me think it's getting normalized (not clamped)
ope, I figured it out. It was the DeadZone::default() I had on there. Of course, that is clamping it. Makes sense now.
you can change the let ... else into ? to clean up the function a bit
fn look(
e: On<Fire<Look>>,
mut players: Query<&mut Transform, With<PlayerAvatar>>,
mut cam_pivot_q: Single<&mut Transform, (With<CameraPivot>, Without<PlayerAvatar>)>,
cursor: Res<Cursor>,
mut shoulders: Query<&mut hands::ShoulderSway>,
mut hands: Query<&mut hands::HandSway>,
) -> Result<(), BevyError> {
// if the cursor is visible, do not look:
if cursor.visible {
return Ok(());
}
let mut transform = players.get_mut(e.context)?;
let mut cam_transform = cam_pivot_q.into_inner();
let delta = e.value * 0.01;
if delta != Vec2::ZERO {
let (yaw, _, _) = transform.rotation.to_euler(EulerRot::YXZ);
let (_, pitch, roll) = cam_transform.rotation.to_euler(EulerRot::YXZ);
let yaw = yaw - delta.x;
const PITCH_LIMIT: f32 = FRAC_PI_2 - 0.01;
let pitch = (pitch - delta.y).clamp(-PITCH_LIMIT, PITCH_LIMIT);
transform.rotation = Quat::from_euler(EulerRot::YXZ, yaw, 0.0, 0.0);
cam_transform.rotation = Quat::from_euler(EulerRot::YXZ, 0.0, pitch, roll);
// pass some delta onto shoulders for sway
for mut sway in &mut shoulders {
sway.yaw.velocity += delta.x * -1.0;
}
for mut sway in &mut hands {
sway.yaw.velocity += delta.x * -1.0;
sway.pitch.velocity += delta.y * -4.0;
sway.roll.velocity += delta.x * -1.0;
}
}
}
it will cause it to panic but you can change the default error handler to prevent that