Hacking Cinemachine

During the development of my most recent side-project I was faced with some problems while trying to create a FPS camera with Cinemachine. What I needed was : A new POV Aim component that allowed limits to the movement and a new Input mode for Rewired axis.

Let me explain what I did.

Step 1: Adding Rewired Support

To achieve this you have to touch upon the AxisState class. Cinemachine treats inputs as AxisState classes, and they are the ones responsible to actually capture the Axis Values and return required information.

I had to add the rewired wrappers to the AxisState constructors and the Update method. This was pretty easy to do.

Step 2: Creating a new AxisState class with Limits that don't wrap

One thing Cinemachine does by default is wrap axis values around their limits, and for a FPS camera this was not very good. This is why I had to create a new class for AxisState that didn't wrap around the axis values.

For this I had to mess with the Update method and tell it to ignore motion if the input goes out of range, instead of wrapping it.

Step 3: Creating a POV Aim module that used my limited AxisState

Since I created a new AxisState class for limited input, I had to create a new Aim Module to use it. I didn't want to mess with other modules as they are really useful and might be used too.

I copied the Aim State and replaced the AxisState with my AxisStateLimited instead. This allowed me to use a POV module that did exactly what I wanted.

This time I needed a way to make the camera orbit around an object with an axis (the mouse or the right stick on a gamepad). I also needed this to be limited.

Step 1: Cloning the Orbital Transposer

I have to admit, every time I try to make something new for Cinemachine I always start by copying whatever is already there that slightly resembles what I want.

This time I started by cloning the Orbital Transposer.

Step 2: Adding limiters

Since the last hack already made sure every input Cinemachine uses is handled by Rewired, I just had to change the new transposer to use a limited axis.

By doing so I also had to change some of the delegates used, but this is mostly finding the right part of code and switching the class of input the delegate uses.

Step 3: Profit

Yeah, this was it. Simple right? I know!

Once again I'm impressed by how easy it is to make Cinemachine my own powerhouse. So here is my proposition to you: Reach out to me on any social media and ask for a Cinemachine extension, I promise I'll work on it when I can.

Edit: Cinemachine v2.1.11 has this functionality built in. Go check that out!

Do you want to use this?

So, I gather that my explanation was not very complete, this is why I'm making a UnityPackage that has these modifications and giving it out for free.