简体   繁体   中英

Wanting to only enable an input if a float is within a certain range

I am a beginner in C# and would like to enable the player to zoom the camera in/out (orthographic) within a certain range (2-7). My code works in terms of zooming in and out, however when the level of zoom reaches 2 or 7 it will not allow any input after.

I apologize in advance if my code is a mess or if the solution is simple, as I have only been at it for a few days.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class CameraZoom : MonoBehaviour
{
    public CinemachineVirtualCamera cvc;
    public float scrollSpeed = 10;

    void Start()
    {
        cvc = GetComponent<CinemachineVirtualCamera>();
    }

    void Update()
    {
        if (Input.GetAxis("Mouse ScrollWheel") != 0f && cvc.m_Lens.OrthographicSize >= 2 && cvc.m_Lens.OrthographicSize <= 7)
        {
            cvc.m_Lens.OrthographicSize -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
        }
        else
        {
            Debug.Log("Cannot zoom any further.");
        }
    }

}

I am assuming cvc.m_Lens.OrthographicSize changes only in the code shown.

This seems to me like a simple issue of logic. Once you set

cvc.m_Lens.OrthographicSize to a value that is >=2 or <=7 

which happens inside your if, the field no longer changes and you cannot get inside the if anymore because your condition is always false.

My recommendation is to determine the proposed new value for cvc.m_Lens.OrthographicSize outside your if, save it into a variable then decide if you want assign. Something like this:

void Update()
{

    // get proposed new size
    var newOrthographicSize = cvc.m_Lens.OrthographicSize - Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;

    if (Input.GetAxis("Mouse ScrollWheel") != 0f {
        Debug.Log("Use scroll wheel to operate.");
        return;
    }

    if (newOrthographicSize >= 2 && newOrthographicSize <= 7)
    {
        cvc.m_Lens.OrthographicSize = newOrthographicSize;
    }
    else
    {
        Debug.Log("Cannot zoom any further.");
    }
}

You will want to separate the logic in three steps

  • get and handle User input
  • ensure the resulting value lies within your range
  • assign the new value

This could eg look like

private void Update ()
{
    // 1. get and handle input
    var newSize = cvc.m_Lens.OrthographicSize - Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;

    // 2. Ensure range
    if(newSize < 2f) newSize = 2f;
    else if(newSize > 7f) newSize = 7f;

    // 3. Assign
    cvc.m_Lens.OrthographicSize = newSize;
}

What you are looking for isMathf.Clamp

you could simply do the entire thing in a single line:

private void Update ()
{
    cvc.m_Lens.OrthographicSize = Mathf.Clamp(cvc.m_Lens.OrthographicSize - Input.GetAxis("Mouse ScrollWheel") * scrollSpeed, 2f, 7f);;
}

making sure that input is always handled but the size guaranteed to stay between 2 and 7 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM