This commit is contained in:
Scove
2026-03-27 20:20:06 +07:00
parent df1377206a
commit ed67b8258d
4 changed files with 82 additions and 30 deletions

View File

@@ -26,7 +26,7 @@ namespace OnlyScove.Scripts
[Header("First Person View Settings")]
[SerializeField] Transform fpvTarget; // Specific transform on the player (e.g., eye level)
[SerializeField] float fpvPositionSmoothTime = 0.05f;
[SerializeField] float fpvRotationSmoothTime = 0.05f;
[SerializeField] float fpvRotationSmoothTime = 20f;
[SerializeField] float fpvFOV = 80f;
[SerializeField] float transitionDuration = 0.3f;
[SerializeField] float tpvBaseFOV = 60f; // Existing base FOV for TPV
@@ -38,6 +38,8 @@ namespace OnlyScove.Scripts
private float _transitionTimer = 0f;
private bool _inTransition = false;
public CameraViewMode CurrentViewMode => _currentViewMode;
// Properties to get current smoothing values based on view mode
private float CurrentPositionSmoothTime => _currentViewMode == CameraViewMode.FirstPerson ? fpvPositionSmoothTime : positionSmoothTime;
private float CurrentRotationSmoothTime => _currentViewMode == CameraViewMode.FirstPerson ? fpvRotationSmoothTime : rotationSmoothTime;
@@ -97,7 +99,7 @@ namespace OnlyScove.Scripts
if (_currentViewMode == CameraViewMode.ThirdPerson)
{
// TPV specific calculations
transform.rotation = rotationHandler.CurrentRotation; // Set rotation from handler
transform.rotation = rotationHandler.CurrentRotation; // Set camera rotation from handler
focusPosition = followTarget.position + rotationHandler.CurrentRotation * new Vector3(framingOffset.x + sideBias.CurrentSideBias, framingOffset.y, 0);
targetDistance = collisionHandler.CheckCollision(focusPosition, rotationHandler.CurrentRotation, zoomHandler.CurrentDistance, zoomHandler.MinDistance);
characterFading.HandleCharacterFading(targetDistance);
@@ -106,8 +108,18 @@ namespace OnlyScove.Scripts
else // FirstPerson
{
// FPV specific calculations
// Rotation is derived from fpvTarget, not input from rotationHandler
transform.rotation = fpvTarget.rotation; // Directly set FPV rotation
// Player's horizontal rotation (body) follows mouse YAW
if (followTarget != null)
{
followTarget.rotation = rotationHandler.PlanarRotation; // Sync body to camera yaw
}
if (fpvTarget != null)
{
fpvTarget.rotation = rotationHandler.CurrentRotation; // Sync head/eyes to full camera rotation
}
transform.rotation = rotationHandler.CurrentRotation; // Set camera rotation from handler (which includes vertical)
focusPosition = fpvTarget.position;
targetDistance = 0; // FPV has no distance to player
@@ -117,8 +129,7 @@ namespace OnlyScove.Scripts
}
// Calculate target position using the currently set transform.rotation
Vector3 targetPosition = focusPosition - transform.rotation * new Vector3(0, 0, targetDistance);
Vector3 targetPosition = focusPosition - transform.rotation * new Vector3(0, 0, targetDistance);
// Handle camera shake
shakeManager.HandleShake();
@@ -131,6 +142,7 @@ namespace OnlyScove.Scripts
if (_inTransition) return; // Prevent multiple toggles during transition
_targetViewMode = (_currentViewMode == CameraViewMode.ThirdPerson) ? CameraViewMode.FirstPerson : CameraViewMode.ThirdPerson;
Debug.Log($"[CameraController] Toggling view from {_currentViewMode} to {_targetViewMode}");
_inTransition = true;
_transitionTimer = 0f;
}
@@ -149,6 +161,13 @@ namespace OnlyScove.Scripts
// TPV -> FPV transition
// Interpolate FOV
_cam.fieldOfView = Mathf.Lerp(dynamicFOV.CurrentTpvBaseFOV, fpvFOV, t);
// Rotate player body to match camera's intended horizontal look direction
if (followTarget != null)
{
followTarget.rotation = Quaternion.Slerp(followTarget.rotation, rotationHandler.PlanarRotation, t);
}
// Interpolate position and rotation
transform.position = Vector3.Lerp(transform.position, fpvTarget.position, t);
transform.rotation = Quaternion.Slerp(transform.rotation, fpvTarget.rotation, t);
@@ -158,23 +177,26 @@ namespace OnlyScove.Scripts
// FPV -> TPV transition
// Interpolate FOV
_cam.fieldOfView = Mathf.Lerp(fpvFOV, dynamicFOV.CurrentTpvBaseFOV, t);
// For position and rotation, we'll let the TPV logic take over after transition.
// If a smooth exit from FPV to TPV is desired for position/rotation,
// more complex calculations would be needed here to store TPV target position/rotation at the start.
}
if (t >= 1f)
{
_currentViewMode = _targetViewMode;
Debug.Log($"[CameraController] View transition complete. Current mode: {_currentViewMode}");
_inTransition = false;
// Re-initialize rotation handler for new view mode context
rotationHandler.Initialize(transform);
// Initialize rotation handler based on new view mode
if (_currentViewMode == CameraViewMode.FirstPerson && fpvTarget != null)
{
rotationHandler.InitializeFPV(fpvTarget); // Initialize FPV rotation handler
}
else
{
rotationHandler.Initialize(transform); // Initialize TPV rotation handler
}
// Ensure FOV is set correctly at the end of transition
_cam.fieldOfView = (_currentViewMode == CameraViewMode.FirstPerson) ? fpvFOV : dynamicFOV.CurrentTpvBaseFOV;
// For FPV, ensure camera matches fpvTarget immediately after transition
// This is already handled by the `transform.position = Vector3.SmoothDamp...` and `transform.rotation = fpvTarget.rotation` in Update
}
}