Search Unity

Question Adding a simple zoom button for a first-person virtual camera

Discussion in 'Cinemachine' started by AtomicLugia, Dec 30, 2020.

  1. AtomicLugia

    AtomicLugia

    Joined:
    Aug 15, 2020
    Posts:
    54
    Warning: Script noob ahead!

    So... I was trying to add a simple zoom function to the Cinemachine Virtual Camera with Bolt, but it seems there's no way to change the camera's FoV via Bolt at all, even after I added Cinemachine in Bolt's Assembly Options. So, my next try was to use a C# script that does this. I got the button check working alright but the Cinemachine thingy doesn't work at all. Here's what I tried:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class CinemachineZoom : MonoBehaviour
    7. {
    8.  
    9.     public float zoom1;
    10.     public float zoom2;
    11.  
    12.     void Update()
    13.     {
    14.         if(Input.GetKey(KeyCode.LeftAlt))
    15.         {
    16.             Debug.Log("Zooming in...");
    17.             vcam.m_lens.FieldOfView = zoom2;
    18.         }
    19.         else
    20.         {
    21.             Debug.Log("Zooming out.");
    22.             vcam.m_lens.FieldOfView = zoom1;
    23.         }
    24.     }
    25. }
    26.  
    I added the script as component directly to the Cinemachine Virtual Camera game object itself.

    When I return to Unity after creating/editing the script, I get these two lovely errors:
    Assets\Scripts\CinemachineZoom.cs(17,13): error CS0103: The name 'vcam' does not exist in the current context
    Assets\Scripts\CinemachineZoom.cs(22,13): error CS0103: The name 'vcam' does not exist in the current context


    So... What did I wrong? Any help would be appreciated!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Add this code to the class, to make vcam a thing:
    Code (CSharp):
    1.     CinemachineVirtualCamera vcam;
    2.  
    3.     void Start()
    4.     {
    5.         vcam = GetComponent<CinemachineVirtualCamera>();
    6.     }
     
    AtomicLugia likes this.
  3. AtomicLugia

    AtomicLugia

    Joined:
    Aug 15, 2020
    Posts:
    54
    Thank you! That did the trick!
     
    Gregoryl likes this.
  4. AtomicLugia

    AtomicLugia

    Joined:
    Aug 15, 2020
    Posts:
    54
    Sorry if I bother again but is there also a way to set the X/Y axis speed when zoomed in? I can't find any settings for it in script...
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    I don't understand. X/Y axis speed of what?
     
  6. AtomicLugia

    AtomicLugia

    Joined:
    Aug 15, 2020
    Posts:
    54
    The look speed, when moving the mouse
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Are you using POV in the vcam?
     
  8. AtomicLugia

    AtomicLugia

    Joined:
    Aug 15, 2020
    Posts:
    54
    Yup. Sorry, I didn't mention that earlier, I'm still kinda new here.
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    vcam.GetCinemachineComponent<CinemachinePOV>().m_HorizontalAxis.m_MaxSpeed
     
    AtomicLugia likes this.
  10. AtomicLugia

    AtomicLugia

    Joined:
    Aug 15, 2020
    Posts:
    54
    Thank you! It works!
     
    Gregoryl likes this.
  11. Kinnith7

    Kinnith7

    Joined:
    Jul 4, 2017
    Posts:
    84
    Hi, I noticed your post. I made this a few days ago if you ever need it. Zoom function without FOV manipulation in Cinemachine.