Forum rules - please read before posting.

Mouse wheel zoom function like in the 3rd person cam for standard game camera

Is it possible to add a mouse wheel zoom function to the standard game camera such as in the 3rd person cam?

Comments

  • Are you looking to move the camera, or change its FOV? The 3rd person camera works by changing the distance to the target.

    The standard GameCamera camera type doesn't accept input, so you'd need to rely on a custom script to have it react to mousewheel input.

    However, since the GameCamera itself positions itself based on other factors, you may find a conflict if you try to also position it with a custom script. What may be the better option would be to instead move the GameCamera's actual Camera component to a child object, and then have a custom script move the child instead - so that the GameCamera can still move freely.

    For example, something like this attached to the child would move it in the x-axis:

    using UnityEngine;
    using AC;
    
    public class ZoomCamera : MonoBehaviour
    {
    
        public float minDistance = -1f;
        public float maxDistance = 1f;
        public float zoomSpeed = 20f;
        public float accelaration = 20f;
    
        private _Camera associatedCamera;
    
    
        private void Awake ()
        {
            associatedCamera = GetComponent <_Camera>();
        }
    
    
        private void Update ()
        {
            if (KickStarter.mainCamera.attachedCamera != associatedCamera) return;
    
            float input = Input.GetAxis ("Mouse ScrollWheel");
    
            float currentDistance = transform.localPosition.x;
            float targetDistance = currentDistance + (input * zoomSpeed * Time.deltaTime);
            float newDistance = Mathf.Lerp (currentDistance, targetDistance, accelaration * Time.deltaTime);
    
            newDistance = Mathf.Clamp (newDistance, minDistance, maxDistance);
    
            transform.localPosition = new Vector3 (newDistance, 0f, 0f);
        }
    
    
    }
    
  • Thank you very much for the input Chris... I played a little with the values and tried all axis, unfortunately, the Z axis is kind of locked while x and y working fine. I will try the FOV approach now.

  • My mistake - that should have been GetComponentInParent:

    using UnityEngine;
    using AC;
    
    public class ZoomCamera : MonoBehaviour
    {
    
        public float minDistance = -1f;
        public float maxDistance = 1f;
        public float zoomSpeed = 20f;
        public float accelaration = 20f;
    
        private _Camera associatedCamera;
    
    
        private void Awake ()
        {
            associatedCamera = GetComponentInParent <_Camera>();
        }
    
    
        private void Update ()
        {
            if (KickStarter.mainCamera.attachedCamera != associatedCamera) return;
    
            float input = Input.GetAxis ("Mouse ScrollWheel");
    
            float currentDistance = transform.localPosition.x;
            float targetDistance = currentDistance + (input * zoomSpeed * Time.deltaTime);
            float newDistance = Mathf.Lerp (currentDistance, targetDistance, accelaration * Time.deltaTime);
    
            newDistance = Mathf.Clamp (newDistance, minDistance, maxDistance);
    
            transform.localPosition = new Vector3 (newDistance, 0f, 0f);
        }
    
    
    }
    
  • edited August 2020

    Thank you very much for the effort Chris. I tried it now in the last days... somehow the Z-Axis Zoom is still not working... It feels like it gets reset each frame to its initial or actual z-position

    Would be really great if you could one day implement inside the Game Camera under Z-Axis_movement an Option called something like Set Influence via Mouse Wheel where it modifies the "Influence" when scrolling the mouse wheel... because when I set the "Affected by:" to "Target Z" and then changing the "Influence" inside the inspector during game play then it is like the zoom I want to achieve.

    I edited then your script and attached it directly to an AC Game Camera:

    using UnityEngine;
    using AC;
    
    public class ZoomCamera : MonoBehaviour
    {
        public float zoomSpeed = 20f;
        public GameCamera MyACCam;
    
        private void Update()
        {
            float input = Input.GetAxis("Mouse ScrollWheel");
            MyACCam.zGradient = input * zoomSpeed * Time.deltaTime;
        }
    
    }
    

    I thought this would make it possible to change the Influence via Mouse Wheel like when I change the "Influence" inside the inspector during game play. But unfortunately, it resets also the float value of "Influence" back to 0 after I scrolled the mouse wheel.

  • Did you use parenting in the way I suggested when using the script? Bear in mind that it also depends on the orientation.

    If you can share screenshots showing the way you've arranged your camera setup (the Hierarchy and Inspectors of both the camera and its parent), I'll recreate it and see what the issue is.

  • edited August 2020

    Yes I did as you recommended but it does not work, please see below my issue:

    1. I created a new empty game object and added the AC GameCamera component to it and renamed it to Zoom.
    2. I removed the GameCamera Component of the AC Camera called LakeCam2 (1) and made it a child of the Zoom GameObject
    3. I added the ZoomCamera script to the LakeCam2 (1)

    In the video, you can see that it works fine in the X-Axis (same accounts for the Y-Axis) but as soon as I change the script to zoom in the Z-Axis then the AC Camera is clamped in its Z-Axis position.

  • Change this:

        float currentDistance = transform.localPosition.x;
    

    to this:

        float currentDistance = transform.localPosition.z;
    
  • I feel so stupid haha... Thank you very much Chris!

  • Hello Ac Community;

    I'm trying to achieve something similar with a 2D camera (specifically a DragCamera2D), and I've been trying to manipulate the camera's Size variable with a variation this script - of course, in 2D, the z transform moving doesn't actually zoom the camera in and out - but I'm having some difficulty finding the exact variable I need to change.

    Any hints from users who've got a mousewheel zoom working in 2D? Am I barking ip the wrong tree with this approach?

    Cheers,

    -Martyn-

  • By "Size", you mean orthographicSize?

    If the Camera is Orthographic, you should be able to update it just fine - but you may need to do so in LateUpdate rather than Update, to avoid conflict with AC.

  • Hello again!

    Yes, I do believe I mean orthographicSize.

    I've been trying to manipulate it with this version of the script you prepared for Vacerias, as a child object of an AC DragCamera2D:

    `
    using UnityEngine;
    using AC;

    public class ZoomCamera : MonoBehaviour
    {

    public float minDistance = 1f;
    public float maxDistance = 5f;
    public float zoomSpeed = 20f;
    public float accelaration = 20f;
    
    private _Camera associatedCamera;
    

    private void Awake()
    {
    associatedCamera = GetComponentInParent<_Camera>();
    }

    private void Update()
    {
        if (KickStarter.mainCamera.attachedCamera != associatedCamera) return;
    
        float input = Input.GetAxis("Mouse ScrollWheel");
    
        float currentDistance = associatedCamera.orthographicSize;
        float targetDistance = currentDistance + (input * zoomSpeed * Time.deltaTime);
        float newDistance = Mathf.Lerp(currentDistance, targetDistance, accelaration * Time.deltaTime);
    
        newDistance = Mathf.Clamp(newDistance, minDistance, maxDistance);
    
        associatedCamera.orthographicSize = newDistance;
    }
    

    }
    `

    As I suspect you might have clocked, this gives a console error:

    Assets\ZoomCamera.cs(27,50): error CS1061: '_Camera' does not contain a definition for 'orthographicSize' and no accessible extension method 'orthographicSize' accepting a first argument of type '_Camera' could be found (are you missing a using directive or an assembly reference?)

    using Camera (without the underscore), since its the camera component not the AC _Camera script I want to affect remedies the error, but doesn't work in terms of any zooming happening, when childed to either the drag camera or the main camera itself.

    I've a suspicion I'm going about this all wrong, but I'm sure it's educational on some level...

    -M-

  • edited October 2021

    _Camera is the base class for all AC GameCameras that the MainCamera can attach itself to.

    To get the Unity Camera component for a given _Camera, read its Camera property:

    associatedCamera.Camera.orthographicSize
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.