Cleaned camera code

This commit is contained in:
MaddoScientisto 2025-12-29 17:39:22 +01:00
commit 1f0ec0c6da

View file

@ -8,9 +8,6 @@ public partial class CameraController3D : Camera3D
[Export] public bool EnableSmoothing = true; [Export] public bool EnableSmoothing = true;
[Export] public bool FollowTargeting = true; [Export] public bool FollowTargeting = true;
[Export] public bool SnapCamera = true;
// [Export] public float CameraSnapStep = 0.02f; // 0 = disabled
[Export] public Vector3 DefaultCameraRotation = new Vector3(-36f, 45f, 0f); [Export] public Vector3 DefaultCameraRotation = new Vector3(-36f, 45f, 0f);
@ -97,89 +94,44 @@ public partial class CameraController3D : Camera3D
_currentPosition = desiredCameraPos; _currentPosition = desiredCameraPos;
} }
// if (CameraSnapStep > 0f) GlobalPosition = _currentPosition;
// {
// _currentPosition = Snap(_currentPosition, CameraSnapStep);
// }
if (SnapCamera)
{
GlobalPosition = SnapToPixelGrid(_currentPosition);
}
else
{
GlobalPosition = _currentPosition;
}
//GlobalPosition = _currentPosition; //GlobalPosition = _currentPosition;
// No LookAt or dynamic rotation — angle is fixed // No LookAt or dynamic rotation — angle is fixed
} }
private Vector3 GetAimOffsetWorldSpace() private Vector3 GetAimOffsetWorldSpace()
{ {
Vector2 stickDir = new Vector2( var stickDir = new Vector2(
Input.GetActionStrength(AimRightName) - Input.GetActionStrength(AimLeftName), Input.GetActionStrength(AimRightName) - Input.GetActionStrength(AimLeftName),
Input.GetActionStrength(AimDownName) - Input.GetActionStrength(AimUpName) Input.GetActionStrength(AimDownName) - Input.GetActionStrength(AimUpName)
); );
float stickLen = stickDir.Length(); var stickLen = stickDir.Length();
if (stickLen > AimDeadzone) if (stickLen > AimDeadzone)
{ {
float scaled = (stickLen - AimDeadzone) / (1f - AimDeadzone); var scaled = (stickLen - AimDeadzone) / (1f - AimDeadzone);
Vector2 aimDir2D = stickDir.Normalized() * Mathf.Clamp(scaled, 0f, 1f); var aimDir2D = stickDir.Normalized() * Mathf.Clamp(scaled, 0f, 1f);
return new Vector3(aimDir2D.X, 0, aimDir2D.Y); return new Vector3(aimDir2D.X, 0, aimDir2D.Y);
} }
// Mouse fallback // Mouse fallback
Vector2 mousePos = GetViewport().GetMousePosition(); var mousePos = GetViewport().GetMousePosition();
Vector3 rayOrigin = ProjectRayOrigin(mousePos); var rayOrigin = ProjectRayOrigin(mousePos);
Vector3 rayDir = ProjectRayNormal(mousePos) * 1000f; var rayDir = ProjectRayNormal(mousePos) * 1000f;
var plane = new Plane(Vector3.Up, 0); var plane = new Plane(Vector3.Up, 0);
var hit = plane.IntersectsRay(rayOrigin, rayDir); var hit = plane.IntersectsRay(rayOrigin, rayDir);
if (hit is Vector3 hitPoint) if (hit is not { } hitPoint) return Vector3.Zero;
{ {
Vector3 offset = hitPoint - _target.GlobalTransform.Origin; var offset = hitPoint - _target.GlobalTransform.Origin;
offset.Y = 0; offset.Y = 0;
float dist = offset.Length(); var dist = offset.Length();
if (dist > 0.01f) if (!(dist > 0.01f)) return Vector3.Zero;
{ var scaled = Mathf.Clamp((dist - AimDeadzone) / (10f - AimDeadzone), 0f, 1f);
float scaled = Mathf.Clamp((dist - AimDeadzone) / (10f - AimDeadzone), 0f, 1f); return offset.Normalized() * scaled;
return offset.Normalized() * scaled;
}
} }
return Vector3.Zero;
} }
private Vector3 SnapToPixelGrid(Vector3 worldPos)
{
var viewport = GetViewport();
float viewportHeight = viewport.GetVisibleRect().Size.Y;
// World units per screen pixel
float unitsPerPixel = (Size * 2f) / viewportHeight;
return new Vector3(
Mathf.Round(worldPos.X / unitsPerPixel) * unitsPerPixel,
Mathf.Round(worldPos.Y / unitsPerPixel) * unitsPerPixel,
Mathf.Round(worldPos.Z / unitsPerPixel) * unitsPerPixel
);
}
// private static float Snap(float value, float step)
// {
// return Mathf.Round(value / step) * step;
// }
//
// private static Vector3 Snap(Vector3 v, float step)
// {
// return new Vector3(
// Snap(v.X, step),
// Snap(v.Y, step),
// Snap(v.Z, step)
// );
// }
} }