Improve diapason
This commit is contained in:
+184
-4
@@ -4,6 +4,7 @@ using UnityEngine.InputSystem;
|
||||
using Unheimlich.Audio;
|
||||
using Unheimlich.Core;
|
||||
using Unheimlich.Utility;
|
||||
using Unheimlich.Gameplay.Player;
|
||||
using InputManager = Unheimlich.Managers.PlayerInputManager;
|
||||
|
||||
namespace Unheimlich.Gameplay.Player
|
||||
@@ -38,11 +39,14 @@ namespace Unheimlich.Gameplay.Player
|
||||
[SerializeField] private float _diapasonTweenDuration = 0.3f;
|
||||
[SerializeField] private float _diapasonHideOffset = -0.5f;
|
||||
[SerializeField] private AnimationCurve _diapasonTweenCurve = AnimationCurve.EaseInOut(0, 0, 1, 1);
|
||||
[SerializeField] private float _vibrationDuration = 15f;
|
||||
|
||||
[Header("Camera Settings")]
|
||||
[SerializeField] private float _cameraPanSpeed = 2f;
|
||||
[SerializeField] private float _cameraPanBorderSize = 0.1f;
|
||||
[SerializeField] private float _maxCameraPanAngle = 15f;
|
||||
[SerializeField] private float _cameraAlignmentSpeed = 3f;
|
||||
[SerializeField] private float _cameraAlignmentThreshold = 0.5f;
|
||||
|
||||
[Header("Visual Feedback")]
|
||||
[SerializeField] private float _vignetteIntensity = 0.5f;
|
||||
@@ -78,8 +82,14 @@ namespace Unheimlich.Gameplay.Player
|
||||
private float _originalAmbientVolume;
|
||||
private Vector3 _diapasonEquippedPosition;
|
||||
private Vector3 _diapasonHiddenPosition;
|
||||
private Vector3 _originalCameraLocalPosition;
|
||||
private Vector3 _originalPlayerPosition;
|
||||
private Vector3 _originalPlayerRotation;
|
||||
private Vector3 _originalCameraLocalRotation;
|
||||
private bool _canUseDiapason;
|
||||
private bool _isFadingOutConcentration;
|
||||
private Coroutine _returnCoroutine;
|
||||
private Coroutine _vibrationCoroutine;
|
||||
|
||||
protected override void OnAwake()
|
||||
{
|
||||
@@ -87,6 +97,15 @@ namespace Unheimlich.Gameplay.Player
|
||||
if (_playerAudioSource == null)
|
||||
_playerAudioSource = gameObject.AddComponent<AudioSource>();
|
||||
|
||||
if (_playerCamera != null)
|
||||
{
|
||||
_originalCameraLocalPosition = _playerCamera.transform.localPosition;
|
||||
_originalCameraLocalRotation = _playerCamera.transform.localEulerAngles;
|
||||
}
|
||||
|
||||
_originalPlayerPosition = transform.position;
|
||||
_originalPlayerRotation = transform.eulerAngles;
|
||||
|
||||
if (_diapasonObject != null)
|
||||
{
|
||||
_diapasonEquippedPosition = _diapasonObject.transform.localPosition;
|
||||
@@ -222,10 +241,11 @@ namespace Unheimlich.Gameplay.Player
|
||||
|
||||
/// <summary>
|
||||
/// Main listening sequence coroutine:
|
||||
/// 1. Concentration phase - player breathes in, diapason animates out
|
||||
/// 2. Volume fades from 30% to 100% during concentration
|
||||
/// 3. After concentration duration, enters listening mode
|
||||
/// 4. Vignette appears, camera pan enabled, cursor shown
|
||||
/// 1. Camera aligns perpendicular to the target object
|
||||
/// 2. Concentration phase - player breathes in, diapason animates out
|
||||
/// 3. Volume fades from 30% to 100% during concentration
|
||||
/// 4. After concentration duration, enters listening mode
|
||||
/// 5. Vignette appears, camera pan enabled, cursor shown
|
||||
/// </summary>
|
||||
private IEnumerator ListeningSequence()
|
||||
{
|
||||
@@ -233,7 +253,11 @@ namespace Unheimlich.Gameplay.Player
|
||||
InputManager.SetContext(InputManager.InputContext.Listening);
|
||||
|
||||
if (_currentTargetEmitter != null)
|
||||
{
|
||||
_currentTargetEmitter.SetConcentrating(true);
|
||||
|
||||
yield return StartCoroutine(AlignCameraToTarget(_currentTargetEmitter.transform));
|
||||
}
|
||||
|
||||
if (_diapasonObject != null)
|
||||
{
|
||||
@@ -281,6 +305,8 @@ namespace Unheimlich.Gameplay.Player
|
||||
/// - Stops emitter listening
|
||||
/// - Hides vignette and cursor
|
||||
/// - Re-enables player movement
|
||||
/// - Restores player position, rotation and camera with lerp
|
||||
/// - Resets diapason animation to idle
|
||||
/// </summary>
|
||||
private void StopListeningMode()
|
||||
{
|
||||
@@ -290,9 +316,21 @@ namespace Unheimlich.Gameplay.Player
|
||||
_listeningCoroutine = null;
|
||||
}
|
||||
|
||||
if (_vibrationCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_vibrationCoroutine);
|
||||
_vibrationCoroutine = null;
|
||||
}
|
||||
|
||||
IsListening = false;
|
||||
IsConcentrating = false;
|
||||
|
||||
if (_returnCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_returnCoroutine);
|
||||
}
|
||||
_returnCoroutine = StartCoroutine(ReturnToOriginalPosition());
|
||||
|
||||
if (_playerAudioSource != null && _playerAudioSource.isPlaying)
|
||||
{
|
||||
TweenHelper.CancelTween(ref _playerAudioFadeTweenId);
|
||||
@@ -317,6 +355,7 @@ namespace Unheimlich.Gameplay.Player
|
||||
if (_diapasonAnimator != null)
|
||||
{
|
||||
_diapasonAnimator.SetBool(ANIM_BOOL_VIBRATING, false);
|
||||
_diapasonAnimator.Play("Idle", 0, 0f);
|
||||
_diapasonAnimator.enabled = false;
|
||||
}
|
||||
|
||||
@@ -364,6 +403,127 @@ namespace Unheimlich.Gameplay.Player
|
||||
_isFadingOutConcentration = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Aligns the player and camera to the target object:
|
||||
/// - Player moves along target's local X axis to align with target center
|
||||
/// - Player rotates to face the target
|
||||
/// - Camera pitch adjusts to look at target center
|
||||
/// </summary>
|
||||
private IEnumerator AlignCameraToTarget(Transform target)
|
||||
{
|
||||
if (_playerCamera == null || target == null)
|
||||
yield break;
|
||||
|
||||
_originalCameraLocalPosition = _playerCamera.transform.localPosition;
|
||||
_originalCameraLocalRotation = _playerCamera.transform.localEulerAngles;
|
||||
_originalPlayerPosition = transform.position;
|
||||
_originalPlayerRotation = transform.eulerAngles;
|
||||
|
||||
Vector3 targetCenter = target.position;
|
||||
Renderer targetRenderer = target.GetComponent<Renderer>();
|
||||
if (targetRenderer != null)
|
||||
{
|
||||
targetCenter = targetRenderer.bounds.center;
|
||||
}
|
||||
else
|
||||
{
|
||||
Collider targetCollider = target.GetComponent<Collider>();
|
||||
if (targetCollider != null)
|
||||
targetCenter = targetCollider.bounds.center;
|
||||
}
|
||||
|
||||
Vector3 startPlayerPos = transform.position;
|
||||
Vector3 startPlayerRotation = transform.eulerAngles;
|
||||
Vector3 startCameraLocalRotation = _playerCamera.transform.localEulerAngles;
|
||||
|
||||
float startPitch = startCameraLocalRotation.x;
|
||||
if (startPitch > 180f)
|
||||
startPitch -= 360f;
|
||||
|
||||
float elapsed = 0f;
|
||||
float duration = 1f / _cameraAlignmentSpeed;
|
||||
|
||||
while (elapsed < duration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float t = Mathf.SmoothStep(0f, 1f, elapsed / duration);
|
||||
|
||||
Vector3 currentCameraPos = _playerCamera.transform.position;
|
||||
Vector3 toTarget = targetCenter - currentCameraPos;
|
||||
Vector3 directionToTarget = toTarget.normalized;
|
||||
|
||||
Quaternion targetRotation = Quaternion.LookRotation(directionToTarget);
|
||||
float targetYaw = targetRotation.eulerAngles.y;
|
||||
float targetPitch = -Mathf.Asin(directionToTarget.y) * Mathf.Rad2Deg;
|
||||
|
||||
Vector3 targetRightAxis = target.right;
|
||||
Vector3 playerToTargetCenter = targetCenter - transform.position;
|
||||
float offsetAlongTargetRight = Vector3.Dot(playerToTargetCenter, targetRightAxis);
|
||||
Vector3 targetPlayerPos = transform.position + targetRightAxis * offsetAlongTargetRight;
|
||||
|
||||
Vector3 currentPlayerPos = Vector3.Lerp(startPlayerPos, targetPlayerPos, t);
|
||||
transform.position = currentPlayerPos;
|
||||
|
||||
float currentYaw = Mathf.LerpAngle(startPlayerRotation.y, targetYaw, t);
|
||||
transform.eulerAngles = new Vector3(0f, currentYaw, 0f);
|
||||
|
||||
float currentPitch = Mathf.LerpAngle(startPitch, targetPitch, t);
|
||||
_playerCamera.transform.localEulerAngles = new Vector3(currentPitch, 0f, 0f);
|
||||
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns player and camera to original position and rotation with smooth lerp.
|
||||
/// </summary>
|
||||
private IEnumerator ReturnToOriginalPosition()
|
||||
{
|
||||
if (_playerCamera == null)
|
||||
yield break;
|
||||
|
||||
Vector3 startPlayerPos = transform.position;
|
||||
Vector3 startPlayerRotation = transform.eulerAngles;
|
||||
Vector3 startCameraLocalPos = _playerCamera.transform.localPosition;
|
||||
Vector3 startCameraLocalRotation = _playerCamera.transform.localEulerAngles;
|
||||
|
||||
float startPitch = startCameraLocalRotation.x;
|
||||
if (startPitch > 180f)
|
||||
startPitch -= 360f;
|
||||
|
||||
float targetPitch = _originalCameraLocalRotation.x;
|
||||
if (targetPitch > 180f)
|
||||
targetPitch -= 360f;
|
||||
|
||||
float elapsed = 0f;
|
||||
float duration = 1f / _cameraAlignmentSpeed;
|
||||
|
||||
while (elapsed < duration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float t = Mathf.SmoothStep(0f, 1f, elapsed / duration);
|
||||
|
||||
transform.position = Vector3.Lerp(startPlayerPos, _originalPlayerPosition, t);
|
||||
|
||||
float currentYaw = Mathf.LerpAngle(startPlayerRotation.y, _originalPlayerRotation.y, t);
|
||||
transform.eulerAngles = new Vector3(0f, currentYaw, 0f);
|
||||
|
||||
float currentPitch = Mathf.LerpAngle(startPitch, targetPitch, t);
|
||||
_playerCamera.transform.localEulerAngles = new Vector3(currentPitch, 0f, 0f);
|
||||
|
||||
_playerCamera.transform.localPosition = Vector3.Lerp(startCameraLocalPos, _originalCameraLocalPosition, t);
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
transform.position = _originalPlayerPosition;
|
||||
transform.eulerAngles = _originalPlayerRotation;
|
||||
_playerCamera.transform.localPosition = _originalCameraLocalPosition;
|
||||
_playerCamera.transform.localEulerAngles = _originalCameraLocalRotation;
|
||||
|
||||
_returnCoroutine = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates cursor position and camera panning during listening mode.
|
||||
/// Supports both gamepad (look input) and mouse (screen edges).
|
||||
@@ -485,6 +645,7 @@ namespace Unheimlich.Gameplay.Player
|
||||
/// - Starts listening sequence on the target emitter
|
||||
/// - Plays diapason particles
|
||||
/// - Plays tap sound
|
||||
/// - Starts vibration timer to stop after duration
|
||||
/// </summary>
|
||||
private void TriggerDiapasonTap()
|
||||
{
|
||||
@@ -492,6 +653,10 @@ namespace Unheimlich.Gameplay.Player
|
||||
{
|
||||
_diapasonAnimator.SetTrigger(ANIM_TRIGGER_TAP);
|
||||
_diapasonAnimator.SetBool(ANIM_BOOL_VIBRATING, true);
|
||||
|
||||
if (_vibrationCoroutine != null)
|
||||
StopCoroutine(_vibrationCoroutine);
|
||||
_vibrationCoroutine = StartCoroutine(StopVibrationAfterDuration());
|
||||
}
|
||||
|
||||
if (_diapasonTapClip != null && _playerAudioSource != null)
|
||||
@@ -510,6 +675,21 @@ namespace Unheimlich.Gameplay.Player
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the diapason vibration animation after the specified duration.
|
||||
/// </summary>
|
||||
private IEnumerator StopVibrationAfterDuration()
|
||||
{
|
||||
yield return new WaitForSeconds(_vibrationDuration);
|
||||
|
||||
if (_diapasonAnimator != null)
|
||||
{
|
||||
_diapasonAnimator.SetBool(ANIM_BOOL_VIBRATING, false);
|
||||
}
|
||||
|
||||
_vibrationCoroutine = null;
|
||||
}
|
||||
|
||||
private void PlayBreathIn()
|
||||
{
|
||||
if (_breathInClip != null && _playerAudioSource != null)
|
||||
|
||||
Reference in New Issue
Block a user