gd: added wall hugging

This commit is contained in:
2025-06-10 11:46:51 +02:00
parent 141688ef32
commit 0c015750fd
5 changed files with 135 additions and 28 deletions

View File

@ -20,7 +20,8 @@ public partial class MoveSystem : Node3D
bool IsOnFloor,
bool IsDead,
bool IsHeadTouchingCeiling,
bool isHanging
bool isHanging,
bool isWallHugging
);
[Export(PropertyHint.Range, "0,20,0.1,or_greater")]
@ -38,6 +39,8 @@ public partial class MoveSystem : Node3D
public float CrouchTransitionSpeed { get; set; } = 20.0f;
[Export(PropertyHint.Range, "0,5,0.1,or_greater")]
public float DoubleJumpSpeedFactor { get; set; } = 2f;
[Export(PropertyHint.Range, "0,1,0.01,or_greater")]
public float WallHugGravityReducingFactor { get; set; } = 0.1f;
private Gravity _gravity;
@ -60,7 +63,7 @@ public partial class MoveSystem : Node3D
public void MoveAround(MoveAroundParameters param)
{
var (delta, movementDirection, isOnFloor, isDead, isHeadTouchingCeiling, isHanging) = param;
var (delta, movementDirection, isOnFloor, isDead, isHeadTouchingCeiling, isHanging, isWallHugging) = param;
var doesCapsuleHaveCrouchingHeight = _capsuleCollider.IsCrouchingHeight();
var doesCapsuleHaveDefaultHeight = _capsuleCollider.IsDefaultHeight();
@ -71,6 +74,14 @@ public partial class MoveSystem : Node3D
_parent.MoveAndSlide();
return;
}
if (isWallHugging)
{
_parent.Velocity = new Vector3(
x: _parent.Velocity.X,
y: _parent.Velocity.Y - _gravity.CalculateGravityForce() * (float)delta * WallHugGravityReducingFactor,
z: _parent.Velocity.Z);
return;
}
// Adding the gravity
if (!isOnFloor)
@ -167,16 +178,16 @@ public partial class MoveSystem : Node3D
}
}
public void Jump(bool isDoubleJump)
public void Jump(bool isDoubleJump, Vector3? jumpDirection = null)
{
var effectiveJumpDirection = jumpDirection ?? Vector3.Up;
var jumpForce = isDoubleJump
? _gravity.CalculateJumpForce() * DoubleJumpSpeedFactor
: _gravity.CalculateJumpForce();
_parent.Velocity = new Vector3(
x: _parent.Velocity.X,
y: jumpForce,
z: _parent.Velocity.Z);
var currentHorizontalVelocity = new Vector3(_parent.Velocity.X, 0, _parent.Velocity.Z);
var jumpVelocity = jumpForce * effectiveJumpDirection;
_parent.Velocity = currentHorizontalVelocity + jumpVelocity;
}
public bool CanMantle()

View File

@ -0,0 +1,45 @@
using Godot;
using System;
using System.Collections.Generic;
using RustyOptions;
namespace Movementtests.systems;
public partial class WallHugSystem : Node3D
{
private List<RayCast3D> _raycasts;
public void Init()
{
_raycasts = new List<RayCast3D>();
_raycasts.Add(GetNode<RayCast3D>("front"));
_raycasts.Add(GetNode<RayCast3D>("back"));
_raycasts.Add(GetNode<RayCast3D>("left"));
_raycasts.Add(GetNode<RayCast3D>("right"));
}
public bool IsWallHugging()
{
foreach (RayCast3D raycast in _raycasts)
{
if (raycast.IsColliding())
{
return true;
}
}
return false;
}
public Option<Vector3> GetWallNormal()
{
foreach (RayCast3D raycast in _raycasts)
{
if (raycast.IsColliding())
{
return raycast.GetCollisionNormal().Some();
}
}
return Option<Vector3>.None;
}
}

View File

@ -0,0 +1 @@
uid://tjiji63wlom5