(UNITY 3D): moving a camera along a rail/predefined path using mouse scroll input

894 Views Asked by At

this is my first post on this community. I searched around for some content to help me with my question, and I've found some stuff that could potentially help me but I just wanted to know if its possible to move a camera along a fixed path using a mouse scroll input?

What I'm thinking of is maybe using the track and dolly feature with cinemachine and perhaps moving to one waypoint with every scroll increment up/down, but as I am still a c# noob I dunno how I can do that.

Here is a reference for the kind of movement I want to do: https://www.sprite.com/zerolimits

Any help will be very much appreciated! Thanks :)

1

There are 1 best solutions below

0
KiynL On

As you said yourself. Create a Dolly Cart in Cinemachine and select the Cart in the Follow section of the virtual camera.

enter image description here

Then just connect a position change code to the dolly cart. In the section below, I have added a simple code that works only with clamp and a more advanced code that captures camera noise and makes it smoother. I hope it solves the problem.

enter image description here

SIMPLE

[SerializeField] private CinemachineDollyCart _dollyCart;
private float y;
private void Update()
{
    y += Input.GetAxis("Mouse ScrollWheel");
    _dollyCart.m_Position = Mathf.Clamp01(y); // In position units of Normalized
}

COMPLETE

public class MouseScrollTracker : MonoBehaviour
{
    [SerializeField] private CinemachineDollyCart _dollyCart;

    private float y;

    [Range(0, 10)]
    [SerializeField] private float sensitivity = 1;
    [Range(1, 30)]
    [SerializeField] private float sharpness = 7;
    private void Update()
    {
        // In Position units of Distance
        y += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
        _dollyCart.m_Position = Mathf.Clamp(
            Mathf.Lerp(_dollyCart.m_Position, y, 
                1-Mathf.Exp(-sharpness*Time.unscaledDeltaTime)), 0, _dollyCart.m_Path.PathLength);
    }
}