Search Unity

Change camera Y limit when input is held?

Discussion in 'Scripting' started by Treasureman, Mar 4, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    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...
    http://forum.unity3d.com/threads/change-mouse-sensitivity-when-an-input-is-held.305183/
     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    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.

    Code (CSharp):
    1. public float yMinLimit;
    2. public float yMaxLimit;
    3. private float yInput;
    4.  
    5. private float yMin;
    6. private float yMax;
    7.  
    8. void Start()
    9. {
    10. yMin = yMinLimit;
    11. yMax = yMaxLimit;
    12. }
    13. void Update()
    14. {
    15. yInput += -Input.GetAxis("Mouse Y");
    16. yInput = Mathf.Clamp(yInput, yMin, yMax);
    17. }
    18.  
    19. public void ChangeLimits(float min, float max)
    20. {
    21. yMin = min;
    22. yMax = max;
    23. }
    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.

    Code (CSharp):
    1. public CameraInputs cameraInputScript;
    2. private float minYStorage;
    3. private float maxYStorage;
    4.  
    5. void Start()
    6. {
    7. minYStorage = cameraInputScript.yMinLimit;
    8. maxYStorage = cameraInputScript.yMaxLimit;
    9. }
    10. void Update()
    11. {
    12. if(Input.GetButton("aim"))
    13. cameraInputScript.ChangeLimits(0,0);
    14. if(Input.GetButtonUp("aim"))
    15. cameraInputScript.ChangeLimits(minYStorage, maxYStorage);
    16.  
    17. }
    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 !