Search Unity

use same button for cameraZoom?

Discussion in '2D' started by RuneShiStorm, Sep 21, 2018.

  1. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Hi again!
    I'm trying to use one button to change the camera zoom from 11 to 16 to 20...
    So, each time I press the button I want it to jump from Zoom 11 -> 16 -> 20 (and back to 6) so its basically rotating...In my current script i've set 11f as "default" and I can change the cameraZoom by pressing 2 different buttons.. Any easy way to make a "rotating schedule" for the camera zoom?

    Code (csharp):
    1.  
    2.     public float camSize = 11f;
    3.  
    4.     void Start()
    5.  
    6.     // Update is called once per frame
    7.     void Update()
    8.     {
    9.         if (Input.GetKey(KeyCode.I))
    10.         {
    11.             camSize = 16f;
    12.         }
    13.         else if (Input.GetKey(KeyCode.O))
    14.         {
    15.             camSize = 20f;
    16.         }
    17.     }
    18.  
    Thanks in advnace!
     
  2. Perkki

    Perkki

    Joined:
    Sep 30, 2017
    Posts:
    15
    For example something like this:

    Code (CSharp):
    1. if (Input.GetKey(KeyCode.R)
    2. {
    3.     if(camSize==6f)
    4.     {
    5.         camSize=11f;
    6.     }
    7.     else if(camSize==11f)
    8.     {
    9.         camSize=16f;
    10.     }
    11.     else if(camSize == 16f)
    12.     {
    13.         camSize=20f;
    14.     }
    15.     else if(camSize == 20f)
    16.     {
    17.         camSize=6f;
    18.     }
    19. }
     
  3. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Oh my god you made that looks so simple (Emoji slapping his face) - that worked perfectly :) Thanks alot!
     
  4. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    You could also do something like:
    Code (CSharp):
    1. float[] _zoomLevels = new float[] { 6, 11, 16, 20 };
    2. int _zoomIndex = 0;
    3.  
    4. if(Input.GetKey(KeyCodes.R)
    5. {
    6.     _zoomIndex++
    7.     if(_zoomIndex >= _zoomLevels.Length)
    8.         _zoomIndex = 0;
    9.     camSize = _zoomLevels[_zoomIndex];
    10. }
     
    RuneShiStorm likes this.
  5. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    thnaks, that look even cleaner!