试图在我的游戏中实现缩放功能,但它不会取消缩放

Posted

技术标签:

【中文标题】试图在我的游戏中实现缩放功能,但它不会取消缩放【英文标题】:Trying to implement a zoom function into my game but it wont unzoom 【发布时间】:2021-11-11 20:52:58 【问题描述】:

您好,我试图在我的游戏中实现缩放功能,但是当我松开缩放键时,它不会让我的 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;

【问题讨论】:

这里只需要隔离摄像头功能,这么大的功能看需要时间,请参考***.com/help/minimal-reproducible-example 我将其更改为仅使用缩放的部分,但我不确定问题出在哪里,所以仍然有大量代码抱歉 【参考方案1】:

您的代码不起作用,因为您的第二个 if 语句位于第一个 if 语句中。您的代码应如下所示:

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));
    

【讨论】:

以上是关于试图在我的游戏中实现缩放功能,但它不会取消缩放的主要内容,如果未能解决你的问题,请参考以下文章

如何在android中实现图像视图的缩放效果?

在unity中实现分页滚动的大小的缩放功能(UGUI)

在传单中获取WMS的最小缩放级别

在我的移动站点上禁用输入标签的自动缩放/字段缩放 - 不禁用所有缩放功能

在Delphi中实现图片的旋转、缩放

canvas中实现旋转缩放的方块