|
|
|
|
@@ -6,6 +6,18 @@ namespace Movementtests.systems;
|
|
|
|
|
|
|
|
|
|
public partial class HeadSystem : Node3D
|
|
|
|
|
{
|
|
|
|
|
public record CameraParameters(
|
|
|
|
|
double Delta,
|
|
|
|
|
Vector2 LookDir,
|
|
|
|
|
Vector3 PlayerInput,
|
|
|
|
|
Vector3 PlayerVelocity,
|
|
|
|
|
Vector3 WallContactPoint,
|
|
|
|
|
float SensitivitMultiplier,
|
|
|
|
|
bool WithCameraJitter,
|
|
|
|
|
bool WithCameraBobbing,
|
|
|
|
|
float BobbingMultiplier,
|
|
|
|
|
float FovMultiplier);
|
|
|
|
|
|
|
|
|
|
private Camera3D _camera;
|
|
|
|
|
private Marker3D _cameraAnchor;
|
|
|
|
|
private AnimationPlayer _animationPlayer;
|
|
|
|
|
@@ -13,25 +25,53 @@ public partial class HeadSystem : Node3D
|
|
|
|
|
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
|
|
|
|
|
public float LookSensitivity { get; set; } = 1f;
|
|
|
|
|
|
|
|
|
|
[ExportGroup("Camera incline")]
|
|
|
|
|
[Export(PropertyHint.Range, "0.1,50,0.1,or_greater")]
|
|
|
|
|
public double CameraInclineAcceleration { get; set; } = 10f;
|
|
|
|
|
|
|
|
|
|
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
|
|
|
|
|
public float WallRunCameraIncline { get; set; } = 5f;
|
|
|
|
|
|
|
|
|
|
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
|
|
|
|
|
public float GroundedCameraIncline { get; set; } = 5f;
|
|
|
|
|
|
|
|
|
|
[ExportGroup("Sliding")]
|
|
|
|
|
[Export(PropertyHint.Range, "0,2,0.1,or_greater")]
|
|
|
|
|
public float SlidingCameraHeightOffset { get; set; } = 1.0f;
|
|
|
|
|
|
|
|
|
|
[Export(PropertyHint.Range, "0,1,0.01,or_greater")]
|
|
|
|
|
public float SlidingJitterFrequency { get; set; } = 0.01f;
|
|
|
|
|
|
|
|
|
|
[Export(PropertyHint.Range, "0,1,0.01,or_greater")]
|
|
|
|
|
public float SlidingJitterAmplitude { get; set; } = 0.1f;
|
|
|
|
|
|
|
|
|
|
private FastNoiseLite _slidingNoise = new FastNoiseLite();
|
|
|
|
|
|
|
|
|
|
[ExportGroup("Bobbing")]
|
|
|
|
|
|
|
|
|
|
private float _bobbingAccumulator; // Constantly increases when player moves in X or/and Z axis
|
|
|
|
|
[Export(PropertyHint.Range, "0,10,0.01,or_greater")]
|
|
|
|
|
public float BobbingFrequency { set; get; } = 2.4f;
|
|
|
|
|
[Export(PropertyHint.Range, "0,0.4,0.01,or_greater")]
|
|
|
|
|
public float BobbingAmplitude { set; get; } = 0.08f;
|
|
|
|
|
|
|
|
|
|
[ExportGroup("FOV")]
|
|
|
|
|
[Export(PropertyHint.Range, "0,180,0.1,degrees")]
|
|
|
|
|
public float BaseFov { get; set; } = 75.0f;
|
|
|
|
|
[Export(PropertyHint.Range, "0,10,0.01,or_greater")]
|
|
|
|
|
public float FovChangeFactor { get; set; } = 1.2f;
|
|
|
|
|
[Export(PropertyHint.Range, "0,10,0.01,or_greater")]
|
|
|
|
|
public float FovChangeSpeed { get; set; } = 6.25f;
|
|
|
|
|
[Export(PropertyHint.Range, "0,100,1,or_greater")]
|
|
|
|
|
public float FovMaxedOutSpeed { get; set; } = 20f;
|
|
|
|
|
|
|
|
|
|
[ExportGroup("First Person rig")]
|
|
|
|
|
private Node3D _fpRig;
|
|
|
|
|
private Vector3 _fpRigInitialPosition;
|
|
|
|
|
[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;
|
|
|
|
|
[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;
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
@@ -39,7 +79,10 @@ public partial class HeadSystem : Node3D
|
|
|
|
|
_camera = GetNode<Camera3D>("CameraSmooth/Camera3D");
|
|
|
|
|
_cameraAnchor = GetNode<Marker3D>("CameraAnchor");
|
|
|
|
|
_animationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_fpRig = GetNode<Node3D>("FPRig");
|
|
|
|
|
_fpRigInitialPosition = _fpRig.GlobalPosition;
|
|
|
|
|
|
|
|
|
|
_slidingNoise.NoiseType = FastNoiseLite.NoiseTypeEnum.Perlin;
|
|
|
|
|
_slidingNoise.SetFrequency(SlidingJitterFrequency);
|
|
|
|
|
}
|
|
|
|
|
@@ -49,8 +92,19 @@ public partial class HeadSystem : Node3D
|
|
|
|
|
_animationPlayer.Play("mantle");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LookAround(double delta, Vector2 lookDir, Vector3 playerInput, Vector3 playerVelocity, Vector3? wallContactPoint = null, float sensitivitMultiplier = 1f, bool isSliding = false)
|
|
|
|
|
public void LookAround(CameraParameters inputs)
|
|
|
|
|
{
|
|
|
|
|
var (delta,
|
|
|
|
|
lookDir,
|
|
|
|
|
playerInput,
|
|
|
|
|
playerVelocity,
|
|
|
|
|
wallContactPoint,
|
|
|
|
|
sensitivitMultiplier,
|
|
|
|
|
withCameraJitter,
|
|
|
|
|
withCameraBobbing,
|
|
|
|
|
bobbingMultiplier,
|
|
|
|
|
fovMultiplier) = inputs;
|
|
|
|
|
|
|
|
|
|
// Horizontal movement of head
|
|
|
|
|
float angleForHorizontalRotation = lookDir.X * LookSensitivity * sensitivitMultiplier;
|
|
|
|
|
RotateY(angleForHorizontalRotation);
|
|
|
|
|
@@ -61,11 +115,11 @@ public partial class HeadSystem : Node3D
|
|
|
|
|
currentCameraRotation.X = Mathf.Clamp(currentCameraRotation.X, Mathf.DegToRad(-90f), Mathf.DegToRad(90f));
|
|
|
|
|
|
|
|
|
|
// Camera incline on Wall and more
|
|
|
|
|
var isWallRunning = wallContactPoint.HasValue && wallContactPoint.Value.Length() > Mathf.Epsilon;
|
|
|
|
|
var isWallRunning = wallContactPoint.Length() > Mathf.Epsilon;
|
|
|
|
|
float cameraIncline;
|
|
|
|
|
if (isWallRunning)
|
|
|
|
|
{
|
|
|
|
|
var directionToWall = (wallContactPoint.Value - GlobalPosition).Normalized();
|
|
|
|
|
var directionToWall = (wallContactPoint - GlobalPosition).Normalized();
|
|
|
|
|
var cameraInclineFactor = ComputeCameraInclineFactor(directionToWall);
|
|
|
|
|
cameraIncline = Mathf.DegToRad(WallRunCameraIncline * cameraInclineFactor);
|
|
|
|
|
}
|
|
|
|
|
@@ -77,7 +131,7 @@ public partial class HeadSystem : Node3D
|
|
|
|
|
currentCameraRotation.Z = (float) Mathf.Lerp(currentCameraRotation.Z, cameraIncline, delta * CameraInclineAcceleration);
|
|
|
|
|
_cameraAnchor.Rotation = currentCameraRotation;
|
|
|
|
|
|
|
|
|
|
if (isSliding)
|
|
|
|
|
if (withCameraJitter)
|
|
|
|
|
{
|
|
|
|
|
_cameraAnchor.Position = Vector3.Down*SlidingCameraHeightOffset;
|
|
|
|
|
float noise1D = _slidingNoise.GetNoise1D(Time.GetTicksMsec());
|
|
|
|
|
@@ -89,7 +143,45 @@ public partial class HeadSystem : Node3D
|
|
|
|
|
_cameraAnchor.Position = Vector3.Zero;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector3 newPositionForCamera = Vector3.Zero;
|
|
|
|
|
Vector3 newPositionForRig = Vector3.Zero;
|
|
|
|
|
if (withCameraBobbing)
|
|
|
|
|
{
|
|
|
|
|
_bobbingAccumulator += (float) delta * playerVelocity.Length();
|
|
|
|
|
|
|
|
|
|
// As the _bobbingAccumulator increases we're changing values for sin and cos functions.
|
|
|
|
|
// Because both of them are just waves, we will be slide up with y and then slide down with y
|
|
|
|
|
// creating bobbing effect. The same works for cos. As the _bobbingAccumulator increases the cos decreases and then increases
|
|
|
|
|
newPositionForCamera.Y = Mathf.Sin(_bobbingAccumulator * BobbingFrequency) * BobbingAmplitude * bobbingMultiplier;
|
|
|
|
|
newPositionForCamera.X = Mathf.Cos(_bobbingAccumulator * BobbingFrequency / 2.0f) * BobbingAmplitude * bobbingMultiplier;
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
|
|
// First person rig adjustments
|
|
|
|
|
_fpRig.GlobalTransform = _cameraAnchor.GetGlobalTransformInterpolated();
|
|
|
|
|
// Apply bobbing
|
|
|
|
|
_fpRig.Position += newPositionForRig;
|
|
|
|
|
|
|
|
|
|
var newRigRotation = _fpRig.Rotation;
|
|
|
|
|
var camTilt = Mathf.Lerp(_fpRig.Rotation.Z, cameraIncline*WeaponMoveRotation, delta*WeaponAdjustmentSpeed);
|
|
|
|
|
newRigRotation.Z = (float) camTilt;
|
|
|
|
|
|
|
|
|
|
// var lookInputLerped = lookDir.Lerp(Vector2.Zero, (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);
|
|
|
|
|
|
|
|
|
|
_fpRig.Rotation = newRigRotation;
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float ComputeCameraInclineFactor(Vector3 direction)
|
|
|
|
|
|