您好,我试图在我的游戏中实现缩放功能,但是当我释放缩放键时,它不会让我的 FOV 回到原来的位置
[Header("Functional Options")]
[SerializeField] private bool canSprint = true;
[SerializeField] private bool canJump = true;
[SerializeField] private bool canCrouch = true;
[SerializeField] private bool canUseHeadbob = true;
[SerializeField] private bool willSlideOnSlopes = true;
[SerializeField] private bool canZoom = true;
[Header("Zoom Parameters")]
[SerializeField] private float timeToZoom = 0.3f;
[SerializeField] private float zoomFOV = 30f;
[SerializeField] private float defaultFOV;
private Coroutine zoomRoutine;
void Awake()
{
playerCamera = GetComponentInChildren<Camera>();
characterController = GetComponent<CharacterController>();
defaultYPos = playerCamera.transform.localPosition.y;
defaultFOV = playerCamera.fieldOfView;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
if (canMove)
{
HandleMovementInput();
HandleMouseLook();
if (canJump)
{
HandleJump();
}
if (canCrouch)
{
HandleCrouch();
}
if (canUseHeadbob)
{
HandleHeadbob();
}
if(canZoom)
{
HandleZoom();
}
ApplyFinalMovements();
}
}
private void HandleZoom()
{
if(Input.GetKeyDown(zoomKey))
{
if(zoomRoutine != null)
{
StopCoroutine(zoomRoutine);
zoomRoutine = null;
}
zoomRoutine = StartCoroutine(toggleZoom(true));
if (Input.GetKeyUp(zoomKey))
{
if (zoomRoutine != null)
{
StopCoroutine(zoomRoutine);
zoomRoutine = null;
}
zoomRoutine = StartCoroutine(toggleZoom(false));
}
}
}
private IEnumerator toggleZoom(bool isEnter)
{
float targetFOV = isEnter ? zoomFOV : defaultFOV;
float startingFOV = playerCamera.fieldOfView;
float timeElapsed = 0;
while(timeElapsed < timeToZoom)
{
playerCamera.fieldOfView = Mathf.Lerp(startingFOV, targetFOV, timeElapsed / timeToZoom);
timeElapsed += Time.deltaTime;
yield return null;
}
playerCamera.fieldOfView = targetFOV;
zoomRoutine = null;
}