Forum rules - please read before posting.

Using mouse wheel to zoom camera in and out

edited June 2023 in Technical Q&A

Hello so in my game I want to be able to zoom in and out using the mouse wheel, as well as use the camera scripts and movement which come with ac
I wrote a script however I feel like it inteferes the current camera scripts which come with ac,
that or I may be a little rusty on the coding,

would appreciate if anyone could have a look at my code give me some pointers how to properly implement this feature . heres what I wrote :

Using UnityEngine;

public class CameraZoom : MonoBehaviour

{
public float zoomSpeed = 1.0f;

void Update()
{
    float scrollWheelInput = Input.GetAxis("Mouse ScrollWheel");
    Camera.main.orthographicSize -= scrollWheelInput * zoomSpeed;
}

}

Thanks guys

I am using Unity version 2022.1.7f1
I am using Adventure creator Version V1.76.3

Comments

  • The issue looks to be to do with its affecting of "Camera.main", which will refer to AC's MainCamera.

    AC's MainCamera works by attaching itself to the active "GameCamera", which involves copying over its values each frame - this includes the orthographic size. What's happening is that this attachment is overriding your script's setting of this value.

    What you'd need to do is instead have the script affect the GameCamera the MainCamera is attached to - since changes made to the GameCamera will then also affect the MainCamera each frame.

    You can do this by replacing Camera.main to have it affect the Camera it's attached to - just be sure to attach the script to the GameCamera you want it to affect:

    using UnityEngine;
    
    public class CameraZoom : MonoBehaviour
    {
        public float zoomSpeed = 1.0f;
    
        void Update()
        {
            float scrollWheelInput = Input.GetAxis("Mouse ScrollWheel");
            GetComponent<Camera> ().orthographicSize -= scrollWheelInput * zoomSpeed;
        }
    
    }
    
  • Thanks Chris ! A lifesaver as usual

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.