From 960ce2a54689448303564cdef46a7ff9806e0732 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 14 Aug 2024 15:02:15 +0200 Subject: [PATCH] fix firstpersson shadow movement --- src/FirstPersonPlayer.cpp | 13 +++++++++++-- src/FirstPersonPlayer.h | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/FirstPersonPlayer.cpp b/src/FirstPersonPlayer.cpp index 81a39a1..5114087 100644 --- a/src/FirstPersonPlayer.cpp +++ b/src/FirstPersonPlayer.cpp @@ -33,6 +33,7 @@ static constexpr float FOV_TRANSITION = 8.0f; static constexpr float MIN_FOV_VELOCITY = 0.5; static constexpr float MAX_FOV_VELOCITY = SPRINT_SPEED * 2.0f; +static const float LerpValue = 0.10; static const float AnimationBlend = 7.0; void FirstPersonPlayer::_bind_methods() {} @@ -47,10 +48,9 @@ void FirstPersonPlayer::_ready() { Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED); } m_Head = Object::cast_to(find_child("Head")); - DEV_ASSERT(m_Head); m_Camera = Object::cast_to(m_Head->find_child("Camera")); - DEV_ASSERT(m_Camera); m_AnimationTree = Object::cast_to(find_child("AnimationTree")); + m_Mesh = Object::cast_to(find_child("Mesh")); } void FirstPersonPlayer::_unhandled_input(const godot::Ref& a_Event) { @@ -104,6 +104,7 @@ void FirstPersonPlayer::UpdateBobbing(float a_Delta) { void FirstPersonPlayer::UpdateCamera(const InputEventMouseMotion& a_Event) { m_Head->rotate_y(-a_Event.get_relative().x * SENSITIVITY); + m_Mesh->rotate_y(-a_Event.get_relative().x * 0.005); m_Camera->rotate_x(-a_Event.get_relative().y * SENSITIVITY); float rotationX = m_Camera->get_rotation().x; @@ -134,6 +135,14 @@ void FirstPersonPlayer::UpdatePosition(float delta) { Math::lerp(static_cast(get_velocity().z), static_cast(direction.z * m_Speed), static_cast(delta * AIR_MOVEMENT))}); } + + + if (!direction.is_zero_approx()) { + godot::Vector3 newRotation = m_Mesh->get_rotation(); + newRotation.y = godot::UtilityFunctions::lerp_angle( + newRotation.y, godot::UtilityFunctions::atan2(get_velocity().x, get_velocity().z), LerpValue); + m_Mesh->set_rotation(newRotation); + } } void FirstPersonPlayer::UpdateFOV(float a_Delta) { diff --git a/src/FirstPersonPlayer.h b/src/FirstPersonPlayer.h index ef25723..3236b30 100644 --- a/src/FirstPersonPlayer.h +++ b/src/FirstPersonPlayer.h @@ -26,6 +26,7 @@ class FirstPersonPlayer : public godot::CharacterBody3D { godot::AnimationTree* m_AnimationTree; godot::Camera3D* m_Camera; godot::Node3D* m_Head; + godot::Node3D* m_Mesh; float m_BobTime; float m_Speed;