Better gravity handling

This commit is contained in:
Marco 2025-06-29 12:35:19 +02:00
commit 16992c2bea
4 changed files with 16 additions and 12 deletions

View file

@ -283,8 +283,8 @@ Speed = 5
StrafeSpeed = 2 StrafeSpeed = 2
Acceleration = 150.0 Acceleration = 150.0
Deceleration = 20.0 Deceleration = 20.0
Gravity = -50.0 Gravity = -20.0
FallSpeed = 100.0 FallSpeed = 2.0
[node name="Storage" type="Node" parent="." node_paths=PackedStringArray("Root")] [node name="Storage" type="Node" parent="." node_paths=PackedStringArray("Root")]
script = ExtResource("6_habpy") script = ExtResource("6_habpy")

View file

@ -23,7 +23,7 @@ public partial class Cutscene : BaseState<PlayerState, CharacterBody3D>
base.EnterState(); base.EnterState();
MainObject.Show(); MainObject.Show();
MainObject.Velocity = Vector3.Zero; MainObject.Velocity = Vector3.Zero;
PlayerStorage.MovementDirection = Vector3.Zero; PlayerStorage.MovementDirection = Vector2.Zero;
} }
public override void ExitState() public override void ExitState()

View file

@ -1,6 +1,8 @@
using Cirno.Scripts.Components.Actors; using Cirno.Scripts.Components.Actors;
using Cirno.Scripts.Components.Actors._3D; using Cirno.Scripts.Components.Actors._3D;
using Cirno.Scripts.Utils;
using Godot; using Godot;
using GodotPlugins.Game;
namespace Cirno.Scripts.Components.FSM._3DPlayer; namespace Cirno.Scripts.Components.FSM._3DPlayer;
@ -67,14 +69,16 @@ public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D
var rotatedMovementDirection = movementInput.Rotated(Mathf.DegToRad(-45f)); var rotatedMovementDirection = movementInput.Rotated(Mathf.DegToRad(-45f));
PlayerStorage.MovementDirection = new Vector3(rotatedMovementDirection.X, 0, rotatedMovementDirection.Y); //PlayerStorage.MovementDirection = new Vector3(rotatedMovementDirection.X, 0, rotatedMovementDirection.Y);
PlayerStorage.MovementDirection = rotatedMovementDirection;
HitboxSpriteProvider.SetVisibility(_isStrafing); HitboxSpriteProvider.SetVisibility(_isStrafing);
} }
public override void PhysicsProcess(double delta) public override void PhysicsProcess(double delta)
{ {
var frameVelocity = MainObject.Velocity; var frameVelocity = MainObject.Velocity.ToVector2();
var frameVelocityY = MainObject.Velocity.Y;
if (_isStrafing) if (_isStrafing)
{ {
@ -83,25 +87,25 @@ public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D
} }
else else
{ {
Vector3 targetVelocity = PlayerStorage.MovementDirection * Speed; Vector2 targetVelocity = PlayerStorage.MovementDirection * Speed;
if (PlayerStorage.MovementDirection != Vector3.Zero) if (PlayerStorage.MovementDirection != Vector2.Zero)
{ {
frameVelocity = frameVelocity.MoveToward(targetVelocity, Acceleration * (float)delta); frameVelocity = frameVelocity.MoveToward(targetVelocity, Acceleration * (float)delta);
} }
else else
{ {
frameVelocity = frameVelocity.MoveToward(Vector3.Zero, Deceleration * (float)delta); frameVelocity = frameVelocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
} }
} }
//MainObject.Velocity += _movementDirection * MovementSpeed; //MainObject.Velocity += _movementDirection * MovementSpeed;
var velocityY = Mathf.Clamp(frameVelocity.Y + Gravity * (float)delta, -FallSpeed, FallSpeed); var velocityY = Mathf.Clamp(frameVelocityY + Gravity * (float)delta, -FallSpeed, FallSpeed);
frameVelocity.Y = velocityY; //frameVelocity.Y = velocityY;
MainObject.Velocity = frameVelocity; MainObject.Velocity = frameVelocity.ToVector3(velocityY);
MainObject.MoveAndSlide(); MainObject.MoveAndSlide();
} }

View file

@ -9,5 +9,5 @@ public partial class IsoPlayerStorageModule : Node
public Vector2 FacingDirection { get; set; } = Vector2.Down; public Vector2 FacingDirection { get; set; } = Vector2.Down;
public Vector2 AimingDirection { get; set; } = Vector2.Down; public Vector2 AimingDirection { get; set; } = Vector2.Down;
public Vector3 MovementDirection { get; set; } = Vector3.Zero; public Vector2 MovementDirection { get; set; } = Vector2.Zero;
} }