제페토룸2
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Unity.XR.CoreUtils;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.Interaction.Toolkit.Locomotion.Movement;
|
||||
@@ -7,6 +9,15 @@ public class PlayerController : MonoBehaviour, ISceneInitializable
|
||||
[Header("점프 설정")]
|
||||
[SerializeField] private float _jumpHeight = 1.2f;
|
||||
|
||||
[Header("시야 가림 렌더러")]
|
||||
[SerializeField] private Renderer SightBlockingRenderer;
|
||||
[SerializeField] private float _fadeSightDuration = 1f; // 시야 가림 페이드에 걸리는 시간(초)
|
||||
|
||||
private static readonly int BaseColorId = Shader.PropertyToID("_BaseColor");
|
||||
private static readonly int ColorId = Shader.PropertyToID("_Color");
|
||||
private Material _sightMaterial;
|
||||
private CancellationTokenSource _fadeSightCts;
|
||||
|
||||
private Vector3 _playerVelocity;
|
||||
private CharacterController _controller;
|
||||
private ContinuousMoveProvider _moveProvider;
|
||||
@@ -86,6 +97,58 @@ public void StandUp()
|
||||
if (_moveProvider != null) _moveProvider.enabled = true; // 이동 잠금 해제
|
||||
}
|
||||
|
||||
// 호출되면 시야 가림 렌더러의 _BaseColor 알파를 현재 값에서 1까지 서서히 올린다(시야 가림).
|
||||
public void FadeSight() => StartSightFade(1f);
|
||||
|
||||
// 호출되면 알파를 현재 값에서 0까지 서서히 내린다(시야 회복).
|
||||
public void ClearSight() => StartSightFade(0f);
|
||||
|
||||
private void StartSightFade(float targetAlpha)
|
||||
{
|
||||
if (SightBlockingRenderer == null)
|
||||
{
|
||||
Debug.LogWarning("[PlayerController] SightBlockingRenderer가 비어 있습니다.");
|
||||
return;
|
||||
}
|
||||
|
||||
// .material 인스턴스를 써서 공유 머티리얼 에셋이 변형되지 않게 한다.
|
||||
if (_sightMaterial == null)
|
||||
_sightMaterial = SightBlockingRenderer.material;
|
||||
|
||||
// 진행 중이던 페이드가 있으면 취소하고 새로 시작
|
||||
_fadeSightCts?.Cancel();
|
||||
_fadeSightCts?.Dispose();
|
||||
_fadeSightCts = new CancellationTokenSource();
|
||||
|
||||
_ = SightFadeAsync(targetAlpha, _fadeSightCts.Token);
|
||||
}
|
||||
|
||||
private async Awaitable SightFadeAsync(float targetAlpha, CancellationToken token)
|
||||
{
|
||||
int propId = _sightMaterial.HasProperty(BaseColorId) ? BaseColorId
|
||||
: _sightMaterial.HasProperty(ColorId) ? ColorId
|
||||
: BaseColorId;
|
||||
|
||||
Color color = _sightMaterial.GetColor(propId);
|
||||
float startAlpha = color.a;
|
||||
float t = 0f;
|
||||
|
||||
try
|
||||
{
|
||||
while (t < _fadeSightDuration)
|
||||
{
|
||||
t += Time.deltaTime;
|
||||
color.a = Mathf.Lerp(startAlpha, targetAlpha, t / _fadeSightDuration);
|
||||
_sightMaterial.SetColor(propId, color);
|
||||
await Awaitable.NextFrameAsync(token);
|
||||
}
|
||||
|
||||
color.a = targetAlpha;
|
||||
_sightMaterial.SetColor(propId, color);
|
||||
}
|
||||
catch (OperationCanceledException) { }
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// 바닥 체크 및 Y축 속도 초기화
|
||||
@@ -98,4 +161,10 @@ private void Update()
|
||||
_playerVelocity.y += gravityValue * Time.deltaTime;
|
||||
_controller.Move(_playerVelocity * Time.deltaTime);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_fadeSightCts?.Cancel();
|
||||
_fadeSightCts?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user