From 35c44d60a45bdb41eb932a0692f92c15929b8520 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 30 Oct 2024 17:25:17 +0100 Subject: [PATCH] Add `AutoGravity` option to `CharacterController` for logic from 4de9e9d918368d34bf112244bacd719862754d30 --- .../Physics/Colliders/CharacterController.cpp | 15 +++++++++++---- .../Physics/Colliders/CharacterController.h | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Source/Engine/Physics/Colliders/CharacterController.cpp b/Source/Engine/Physics/Colliders/CharacterController.cpp index d4627cc5f..b554776cd 100644 --- a/Source/Engine/Physics/Colliders/CharacterController.cpp +++ b/Source/Engine/Physics/Colliders/CharacterController.cpp @@ -131,6 +131,11 @@ void CharacterController::SetMinMoveDistance(float value) _minMoveDistance = Math::Max(value, 0.0f); } +void CharacterController::SetAutoGravity(bool value) +{ + _autoGravity = value; +} + Vector3 CharacterController::GetVelocity() const { return _controller ? PhysicsBackend::GetRigidDynamicActorLinearVelocity(PhysicsBackend::GetControllerRigidDynamicActor(_controller)) : Vector3::Zero; @@ -262,10 +267,12 @@ void CharacterController::UpdateBounds() void CharacterController::AddMovement(const Vector3& translation, const Quaternion& rotation) { Vector3 displacement = translation; - - // Apply gravity - const float deltaTime = Time::GetCurrentSafe()->DeltaTime.GetTotalSeconds(); - displacement += GetPhysicsScene()->GetGravity() * deltaTime; + if (_autoGravity) + { + // Apply gravity + const float deltaTime = Time::GetCurrentSafe()->DeltaTime.GetTotalSeconds(); + displacement += GetPhysicsScene()->GetGravity() * deltaTime; + } Move(displacement); diff --git a/Source/Engine/Physics/Colliders/CharacterController.h b/Source/Engine/Physics/Colliders/CharacterController.h index 540adc5c9..0f22440dd 100644 --- a/Source/Engine/Physics/Colliders/CharacterController.h +++ b/Source/Engine/Physics/Colliders/CharacterController.h @@ -65,6 +65,7 @@ private: float _height; float _minMoveDistance; bool _isUpdatingTransform; + bool _autoGravity = false; Vector3 _upDirection; Vector3 _gravityDisplacement; NonWalkableModes _nonWalkableMode; @@ -148,6 +149,20 @@ public: /// API_PROPERTY() void SetMinMoveDistance(float value); + /// + /// Gets the automatic gravity force applying mode. Can be toggled off if gameplay controls character movement velocity including gravity, or toggled on if gravity should be applied together with root motion from animation movement. + /// + API_PROPERTY(Attributes="EditorOrder(250), DefaultValue(false), EditorDisplay(\"Character Controller\")") + bool GetAutoGravity() const + { + return _autoGravity; + } + + /// + /// Sets the automatic gravity force applying mode. Can be toggled off if gameplay controls character movement velocity including gravity, or toggled on if gravity should be applied together with root motion from animation movement. + /// + API_PROPERTY() void SetAutoGravity(bool value); + public: /// /// Gets the linear velocity of the Character Controller. This allows tracking how fast the character is actually moving, for instance when it is stuck at a wall this value will be the near zero vector.