Search code examples
c#unity-game-enginezooming

unity cannot implicitly convert type float' to bool' also Make the camera zoom?


I use C# and I'm new. I am trying to make a zoom function for a game on unity. Here is my code (that I have so far):

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

public class Zoom : MonoBehaviour
{
    public Camera cam;
    public float zoom_speed = 20f;

    // Update is called once per frame
    void Update ()
    {
        if(Input.GetAxis("Mouse ScrollWheel"))
        {
            cam.fieldOfView = zoom_speed;
        }
    }
}

However, I am getting the error "cannot implicitly convert type float to bool when I hover over if(Input.GetAxis("Mouse Scrollwheel")) Also any advice on how to make the zoom program work would be much appreciated.


Solution

  • The Input.GetAxis("Mouse ScrollWheel"); script return a float value.

    The value will be in the range -1...1 for keyboard and joystick input. If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1...1.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Zoom : MonoBehaviour
    {
        public Camera cam;
        public float zoom_speed = 20f;
    
        // Update is called once per frame
        void Update ()
        {
             float d = Input.GetAxis("Mouse ScrollWheel");
             if (d > 0f)
             {
                //Positive value
                //Scroll up
                cam.fieldOfView += zoom_speed;
             }
             else if (d < 0f)
             {
                //Negative value
                //Scroll down
                cam.fieldOfView -= zoom_speed;
             }
    
        }
    }