probably fixed stuttering of the camera and weapon animations

This commit is contained in:
2026-03-18 11:02:08 +01:00
parent 50de6abb5d
commit e51ef5a517
6 changed files with 112 additions and 114 deletions

View File

@@ -39,12 +39,12 @@ public partial class HeadSystem : Node3D
float BobbingMultiplier,
float FovMultiplier);
internal Camera3D _camera;
internal Marker3D _cameraAnchor;
internal AnimationPlayer _animationPlayer;
internal AnimationTree _animationTree;
internal Camera3D Camera = null!;
internal Node3D CameraAnchor = null!;
internal AnimationPlayer AnimationPlayer = null!;
internal AnimationTree AnimationTree = null!;
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
[Export(PropertyHint.Range, "0,1,0.01,or_greater")]
public float LookSensitivity { get; set; } = 1f;
[ExportGroup("Camera incline")]
@@ -84,78 +84,75 @@ public partial class HeadSystem : Node3D
public float FovMaxedOutSpeed { get; set; } = 20f;
[ExportGroup("First Person rig")]
internal Node3D _fpRig;
internal Node3D _rightHandedWeapon;
internal Node3D _leftHandedWeapon;
internal Node3D _fpDisplacedRig;
internal Vector3 _fpDisplacedRigInitialRotation;
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
public float WeaponSway { get; set; } = 5f;
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
public float WeaponLookRotation { get; set; } = 1f;
internal Node3D FpRig = null!;
internal Node3D RightHandedWeapon = null!;
internal Vector3 RightHandedWeaponInitialRotation = Vector3.Zero;
internal Node3D LeftHandedWeapon = null!;
internal Vector3 LeftHandedWeaponInitialRotation = Vector3.Zero;
[Export(PropertyHint.Range, "0,20,1,or_greater")]
public float WeaponSway { get; set; } = 15f;
[Export(PropertyHint.Range, "0,200,1,or_greater")]
public float WeaponMoveRotation { get; set; } = 80f;
[Export(PropertyHint.Range, "0,20,0.1,or_greater")]
public float WeaponAdjustmentSpeed { get; set; } = 10f;
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
public float DisplacedWeaponSway { get; set; } = 5f;
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
public float DisplacedWeaponLookRotation { get; set; } = 1f;
[Export(PropertyHint.Range, "0,1,0.01,or_greater")]
[Export(PropertyHint.Range, "0,20,1,or_greater")]
public float WeaponAdjustmentSpeed { get; set; } = 1f;
[Export(PropertyHint.Range, "0,2,0.01,or_greater")]
public float DisplacedWeaponSway { get; set; } = 0.8f;
[Export(PropertyHint.Range, "0,0.5,0.01,or_greater")]
public float DisplacedWeaponMoveRotation { get; set; } = 0.1f;
[Export(PropertyHint.Range, "0,20,0.1,or_greater")]
public float DisplacedWeaponAdjustmentSpeed { get; set; } = 10f;
[Export(PropertyHint.Range, "0,20,1,or_greater")]
public float DisplacedWeaponAdjustmentSpeed { get; set; } = 12f;
public void Init()
{
_isPlayingForcingAnim = false;
IsPlayingForcingAnim = false;
Input.SetMouseMode(Input.MouseModeEnum.Captured);
_camera = GetNode<Camera3D>("CameraSmooth/Camera3D");
_cameraAnchor = GetNode<Marker3D>("CameraAnchor");
_animationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
_animationTree = GetNode<AnimationTree>("AnimationTree");
_fpRig = GetNode<Node3D>("FPRig");
_rightHandedWeapon = GetNode<Node3D>("FPRig/Sword");
_leftHandedWeapon = GetNode<Node3D>("FPRig/Parry");
_fpDisplacedRig = GetNode<Node3D>("FPRig/Sword");
_fpDisplacedRigInitialRotation = _fpDisplacedRig.Rotation;
Camera = GetNode<Camera3D>("CameraSmooth/Camera3D");
CameraAnchor = GetNode<Node3D>("CameraSmooth/CameraAnchor");
//_cameraAnchor = GetNode<Camera3D>("CameraSmooth/Camera3D");
AnimationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
AnimationTree = GetNode<AnimationTree>("AnimationTree");
FpRig = GetNode<Node3D>("FPRig");
RightHandedWeapon = GetNode<Node3D>("FPRig/Sword/SwordMesh");
RightHandedWeaponInitialRotation = RightHandedWeapon.Rotation;
LeftHandedWeapon = GetNode<Node3D>("FPRig/Parry/ParryMesh");
LeftHandedWeaponInitialRotation = LeftHandedWeapon.Rotation;
_slidingNoise.NoiseType = FastNoiseLite.NoiseTypeEnum.Perlin;
_slidingNoise.SetFrequency(SlidingJitterFrequency);
}
public void SetWeaponsVisible(bool swordVisible, bool parryVisible)
{
_rightHandedWeapon.Visible = swordVisible;
_leftHandedWeapon.Visible = parryVisible;
RightHandedWeapon.Visible = swordVisible;
LeftHandedWeapon.Visible = parryVisible;
}
public void OnMantle()
{
_animationTree.Set("parameters/OnMantle/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
AnimationTree.Set("parameters/OnMantle/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
}
public void OnJumpStarted()
{
_animationTree.Set("parameters/OnJumpStart/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
AnimationTree.Set("parameters/OnJumpStart/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
}
public void OnJumpEnded()
{
_animationTree.Set("parameters/OnJumpEnd/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
AnimationTree.Set("parameters/OnJumpEnd/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
}
public void OnHit()
{
_animationTree.Set("parameters/OnHit/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
AnimationTree.Set("parameters/OnHit/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
}
public void OnParry()
{
_animationTree.Set("parameters/OnParry/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
AnimationTree.Set("parameters/OnParry/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
}
public void OnStartDeathAnimation()
{
_isPlayingForcingAnim = true;
_animationTree.Set("parameters/OnDie/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
IsPlayingForcingAnim = true;
AnimationTree.Set("parameters/OnDie/request", (int) AnimationNodeOneShot.OneShotRequest.Fire);
}
public void OnDeathAnimationFinished()
@@ -190,8 +187,8 @@ public partial class HeadSystem : Node3D
EmitSignalHitboxDeactivated();
}
internal bool _footstepEmitted;
internal bool _isPlayingForcingAnim;
internal bool FootstepEmitted;
internal bool IsPlayingForcingAnim;
public void ResetHeadBobbing()
{
@@ -200,10 +197,10 @@ public partial class HeadSystem : Node3D
public void LookAround(CameraParameters inputs)
{
if (_isPlayingForcingAnim)
if (IsPlayingForcingAnim)
{
_camera.Position = Vector3.Zero;
_camera.Rotation = Vector3.Zero;
Camera.Position = Vector3.Zero;
Camera.Rotation = Vector3.Zero;
return;
}
@@ -223,7 +220,7 @@ public partial class HeadSystem : Node3D
RotateY(angleForHorizontalRotation);
// Vertical movement of head
Vector3 currentCameraRotation = _cameraAnchor.Rotation;
Vector3 currentCameraRotation = CameraAnchor.Rotation;
currentCameraRotation.X += Convert.ToSingle(lookDir.Y * LookSensitivity * sensitivitMultiplier);
currentCameraRotation.X = Mathf.Clamp(currentCameraRotation.X, Mathf.DegToRad(-90f), Mathf.DegToRad(90f));
@@ -242,18 +239,18 @@ public partial class HeadSystem : Node3D
cameraIncline = Mathf.DegToRad(GroundedCameraIncline * cameraInclineFactor * -1.0f);
}
currentCameraRotation.Z = (float) Mathf.Lerp(currentCameraRotation.Z, cameraIncline, delta * CameraInclineAcceleration);
_cameraAnchor.Rotation = currentCameraRotation;
CameraAnchor.Rotation = currentCameraRotation;
if (withCameraJitter)
{
_cameraAnchor.Position = Vector3.Down*SlidingCameraHeightOffset;
CameraAnchor.Position = Vector3.Down*SlidingCameraHeightOffset;
float noise1D = _slidingNoise.GetNoise1D(Time.GetTicksMsec());
float noiseAmplitude = SlidingJitterAmplitude*Mathf.Clamp(playerVelocity.Length(), 0f, 1f);
_cameraAnchor.Position += Vector3.Up*noise1D*noiseAmplitude;
CameraAnchor.Position += Vector3.Up*noise1D*noiseAmplitude;
}
else
{
_cameraAnchor.Position = Vector3.Zero;
CameraAnchor.Position = Vector3.Zero;
}
Vector3 newPositionForCamera = Vector3.Zero;
@@ -268,76 +265,79 @@ public partial class HeadSystem : Node3D
newPositionForCamera.Y = Mathf.Sin(_bobbingAccumulator * BobbingFrequency) * BobbingAmplitude * bobbingMultiplier;
newPositionForCamera.X = Mathf.Cos(_bobbingAccumulator * BobbingFrequency / 2.0f) * BobbingAmplitude * bobbingMultiplier;
if (newPositionForCamera.Y < -0.07 && !_footstepEmitted) Footstep();
if (newPositionForCamera.Y > 0) _footstepEmitted = false;
if (newPositionForCamera.Y < -0.07 * bobbingMultiplier && !FootstepEmitted) Footstep();
if (newPositionForCamera.Y > 0) FootstepEmitted = false;
// Offset bobbing for weapon rig
newPositionForRig.Y = Mathf.Cos(_bobbingAccumulator * BobbingFrequency) * BobbingAmplitude * bobbingMultiplier * 0.2f;
newPositionForRig.X = Mathf.Sin(_bobbingAccumulator * BobbingFrequency / 2.0f) * BobbingAmplitude * bobbingMultiplier * 0.2f;
}
_cameraAnchor.Position += newPositionForCamera;
_camera.GlobalTransform = _cameraAnchor.GetGlobalTransformInterpolated();
CameraAnchor.Position += newPositionForCamera;
Camera.GlobalTransform = CameraAnchor.GetGlobalTransformInterpolated();
// First person rig adjustments
_fpRig.GlobalTransform = _cameraAnchor.GetGlobalTransformInterpolated();
FpRig.GlobalTransform = Camera.GlobalTransform;
// Apply bobbing
_fpRig.Position += newPositionForRig;
FpRig.Position += newPositionForRig;
// Rotate the whole rig based on movement input
var newRigRotation = _fpRig.Rotation;
var camTilt = Mathf.Lerp(_fpRig.Rotation.Z, cameraIncline*WeaponMoveRotation, delta*WeaponAdjustmentSpeed);
var newRigRotation = FpRig.Rotation;
var camTilt = Mathf.Lerp(FpRig.Rotation.Z, CameraAnchor.Rotation.Z*WeaponMoveRotation, delta * WeaponAdjustmentSpeed);
newRigRotation.Z = (float) camTilt;
// Rotate the whole rig based on camera rotation input
newRigRotation.X = Mathf.Lerp(newRigRotation.X, -lookDir.Y*WeaponSway, (float) delta*WeaponAdjustmentSpeed);
newRigRotation.Y = Mathf.Lerp(newRigRotation.Y, -lookDir.X*WeaponSway, (float) delta*WeaponAdjustmentSpeed);
newRigRotation.X = Mathf.Lerp(newRigRotation.X, -lookDir.Y*WeaponSway, (float) delta * WeaponAdjustmentSpeed);
newRigRotation.Y = Mathf.Lerp(newRigRotation.Y, -lookDir.X*WeaponSway, (float) delta * WeaponAdjustmentSpeed);
// Apply
_fpRig.Rotation = newRigRotation;
FpRig.Rotation = newRigRotation;
// Compute displaced rig adjustments, starting with movement input
var newDisplacedRigRotation = _fpDisplacedRig.Rotation;
var howMuchForward = ComputeHowMuchInputForward(playerInput);
var howMuchSideways = ComputeHowMuchInputSideways(playerInput);
var displacedCamTiltForward = Mathf.Lerp(newDisplacedRigRotation.Z,
_fpDisplacedRigInitialRotation.Z + howMuchForward*DisplacedWeaponMoveRotation,
delta*DisplacedWeaponAdjustmentSpeed);
var displacedCamTiltSide = Mathf.Lerp(newDisplacedRigRotation.X,
_fpDisplacedRigInitialRotation.X - howMuchSideways*DisplacedWeaponMoveRotation,
delta*DisplacedWeaponAdjustmentSpeed);
newDisplacedRigRotation.X = (float) displacedCamTiltSide;
newDisplacedRigRotation.Z = (float) displacedCamTiltForward;
var displacedSwayY = Mathf.Lerp(newDisplacedRigRotation.Y,
_fpDisplacedRigInitialRotation.Y - lookDir.X*DisplacedWeaponSway,
delta*DisplacedWeaponAdjustmentSpeed);
newDisplacedRigRotation.Y = (float) displacedSwayY;
// Apply
_fpDisplacedRig.Rotation = newDisplacedRigRotation;
// Compute sword meshes procedural adjustments
RightHandedWeapon.Rotation = ComputeRotationForFpMesh(RightHandedWeapon, RightHandedWeaponInitialRotation, playerInput, lookDir, (float) delta);
LeftHandedWeapon.Rotation = ComputeRotationForFpMesh(LeftHandedWeapon, LeftHandedWeaponInitialRotation, playerInput, lookDir, (float) delta);
// Camera adjustments
float velocityClamped = Mathf.Clamp(playerVelocity.Length(), 0.5f, FovMaxedOutSpeed);
float targetFov = BaseFov + FovChangeFactor * velocityClamped * fovMultiplier;
_camera.Fov = Mathf.Lerp(_camera.Fov, targetFov, (float) delta * FovChangeSpeed);
Camera.Fov = Mathf.Lerp(Camera.Fov, targetFov, (float) delta * FovChangeSpeed);
}
public Vector3 ComputeRotationForFpMesh(Node3D mesh, Vector3 initialRotation, Vector3 playerInput, Vector2 lookDir, float delta)
{
var newMeshRotation = mesh.Rotation;
var howMuchForward = ComputeHowMuchInputForward(playerInput);
var howMuchSideways = ComputeHowMuchInputSideways(playerInput);
var displacedCamTiltForward = Mathf.Lerp(newMeshRotation.Z,
initialRotation.Z + howMuchForward*DisplacedWeaponMoveRotation,
delta * DisplacedWeaponAdjustmentSpeed);
var displacedCamTiltSide = Mathf.Lerp(newMeshRotation.X,
initialRotation.X - howMuchSideways*DisplacedWeaponMoveRotation,
delta * DisplacedWeaponAdjustmentSpeed);
newMeshRotation.X = displacedCamTiltSide;
newMeshRotation.Z = displacedCamTiltForward;
var displacedSwayY = Mathf.Lerp(newMeshRotation.Y,
initialRotation.Y - lookDir.X*DisplacedWeaponSway,
delta * DisplacedWeaponAdjustmentSpeed);
newMeshRotation.Y = displacedSwayY;
return newMeshRotation;
}
public void Footstep()
{
_footstepEmitted = true;
FootstepEmitted = true;
EmitSignalStepFoot();
}
public void HideWeapon()
{
_rightHandedWeapon.Visible = false;
RightHandedWeapon.Visible = false;
}
public void ShowWeapon()
{
_rightHandedWeapon.Visible = true;
RightHandedWeapon.Visible = true;
}
public float ComputeCameraInclineFactor(Vector3 direction)
@@ -368,15 +368,15 @@ public partial class HeadSystem : Node3D
public Vector3 GetGlobalForwardVector()
{
return _camera.GlobalBasis.Z;
return Camera.GlobalBasis.Z;
}
public Vector3 GetGlobalLookRotation()
{
return new Vector3(
_camera.Rotation.X,
Camera.Rotation.X,
Rotation.Y,
_camera.Rotation.Z);
Camera.Rotation.Z);
}
public void SetHeight(float height)