Fix changing CharacterController center at runtime to maintain actor placement

This commit is contained in:
Wojtek Figat
2025-06-05 12:40:47 +02:00
parent fe98a23cb4
commit 1eaf40f2f7
2 changed files with 16 additions and 1 deletions

View File

@@ -292,6 +292,21 @@ RigidBody* CharacterController::GetAttachedRigidBody() const
return nullptr; return nullptr;
} }
void CharacterController::SetCenter(const Vector3& value)
{
if (value == _center)
return;
Vector3 delta = value - _center;
_center = value;
if (_controller)
{
// Change physics position while maintaining actor placement
Vector3 position = PhysicsBackend::GetControllerPosition(_controller);
position += _upDirection * delta;
PhysicsBackend::SetControllerPosition(_controller, position);
}
}
void CharacterController::OnActiveTransformChanged() void CharacterController::OnActiveTransformChanged()
{ {
if (!_shape) if (!_shape)

View File

@@ -61,7 +61,7 @@ public:
/// <summary> /// <summary>
/// Sets the center of the collider, measured in the object's local space. /// Sets the center of the collider, measured in the object's local space.
/// </summary> /// </summary>
API_PROPERTY() void SetCenter(const Vector3& value); API_PROPERTY() virtual void SetCenter(const Vector3& value);
/// <summary> /// <summary>
/// Gets the contact offset. Colliders whose distance is less than the sum of their ContactOffset values will generate contacts. The contact offset must be positive. Contact offset allows the collision detection system to predictively enforce the contact constraint even when the objects are slightly separated. /// Gets the contact offset. Colliders whose distance is less than the sum of their ContactOffset values will generate contacts. The contact offset must be positive. Contact offset allows the collision detection system to predictively enforce the contact constraint even when the objects are slightly separated.