# Change camera Y limit when input is held?

I have a camera with its YMin Limit set to -40 and its YMax Limit set to 50. But I need a script that changes both the Min and Max Y limits on the camera to 0 when the input ‘Aim’ is held. I’ve never really worked with this kind of thing before and have no clue where to start. I would prefer if someone could paste a script into the forum so I could reverse engineer it because that’s always been how I learned best. I could also settle with a link to the unity docs. Thanks!

Also, check out this forum I posted asking for more help on camera stuff…

Well it’s not a big deal. You just have to think step by step. To change those values, first you have to reach them right ? There are several ways to reach to a variable, using a level manager that is supported by singleton pattern, using a public variable and reaching it through a defined class variable, using static values. But in this case, I believe using public variable will fit best for your needs. Let’s assume there is a script called CameraInputs, this will hold the values and limit them with your desired values.

``````public float yMinLimit;
public float yMaxLimit;
private float yInput;

private float yMin;
private float yMax;

void Start()
{
yMin = yMinLimit;
yMax = yMaxLimit;
}
void Update()
{
yInput += -Input.GetAxis("Mouse Y");
yInput = Mathf.Clamp(yInput, yMin, yMax);
}

public void ChangeLimits(float min, float max)
{
yMin = min;
yMax = max;
}
``````

So here we have two values called yMinLimit and yMaxLimit, these are created just so that we can adjust our first desired values over the inspector. But you realized that I actually used other two variables called yMin and yMax to clamp it, that’s because we’ll be changing those variables to another desired value over the ChangeLimits function. In Start function, we set those variables to the ones that we adjusted from the inspector. But as I said, we’ll be changing them into our needs with the ChangeLimits function.

``````public CameraInputs cameraInputScript;
private float minYStorage;
private float maxYStorage;

void Start()
{
minYStorage = cameraInputScript.yMinLimit;
maxYStorage = cameraInputScript.yMaxLimit;
}
void Update()
{
if(Input.GetButton("aim"))
cameraInputScript.ChangeLimits(0,0);
if(Input.GetButtonUp("aim"))
cameraInputScript.ChangeLimits(minYStorage, maxYStorage);

}
``````

So this script above, first it defines the CameraInputs script, you need to drag&drop the object that holds it through the inspector, then it stores the original limit values which you adjusted from your inspector, in order to return the variable back to their original values once the button is released. Lastly, as you can see, it sets the variables to 0 when the button is hold, then back to their original values once the button is released.

Hope that I could’ve helped !