rokojori_action_library/Runtime/Interactions/CharacterController/MoveAndSlide.cs

80 lines
2.0 KiB
C#
Raw Normal View History

2024-12-01 17:07:41 +00:00
using Godot;
using System.Collections;
using System.Collections.Generic;
using Godot.Collections;
namespace Rokojori
{
2025-05-21 20:44:28 +00:00
[Tool]
2025-05-27 06:51:48 +00:00
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/CCMoveAndSlide.svg")]
2024-12-01 17:07:41 +00:00
public partial class MoveAndSlide:CharacterControllerAction
{
2025-12-10 14:17:07 +00:00
[Export]
public bool useStepping = false;
[Export]
public float stepMaxHeight = 0.2f;
2025-01-08 18:46:17 +00:00
protected override void _OnTrigger()
2024-12-01 17:07:41 +00:00
{
2025-12-10 14:17:07 +00:00
var body = controller.body;
var motion = controller.body.Velocity * controller.delta;
var collision = body.MoveAndCollide( motion, testOnly: true );
if ( collision != null && StepUp( motion ) )
{
return;
}
2024-12-01 17:07:41 +00:00
controller.body.MoveAndSlide();
}
2025-12-10 14:17:07 +00:00
protected bool StepUp( Vector3 motion )
{
var up = Vector3.Up * stepMaxHeight;
var hitAbove = controller.body.TestMove( controller.body.GlobalTransform, up );
if ( hitAbove )
{
return false;
}
bool hitForward = controller.body.TestMove( controller.body.GlobalTransform.Translated( up ), motion );
if ( ! hitForward )
{
var space = controller.body.GetWorld3D().DirectSpaceState;
var origin = controller.body.GlobalPosition;
var stepUpOrigin = controller.body.GlobalPosition + up;
var downHit = space.IntersectRay(
new PhysicsRayQueryParameters3D
{
From = stepUpOrigin,
To = stepUpOrigin + Vector3.Down * ( stepMaxHeight + 0.1f ),
CollisionMask = 1,
CollideWithAreas = false,
CollideWithBodies = true
}
);
if ( downHit.Count == 0 )
{
return false;
}
var exactStepAmount = downHit[ "position" ].AsVector3().Y - origin.Y;
if ( exactStepAmount > 0 && exactStepAmount <= stepMaxHeight )
{
controller.body.GlobalTranslate( new Vector3( 0, exactStepAmount, 0 ) );
return true;
}
}
return false;
}
2024-12-01 17:07:41 +00:00
}
}