Input Controls Camera on a Predetermined Path.

Hi, I’m still relatively new to Unity, and this is my first question, so I’ll try to get it across clearly.

The game I’m working on has the player in a vehicle, looking from a first person perspective. I’m trying to allow the player to lean out of the window so that they can look to their sides or behind them. I first achieved this simply allowing the player to rotate the camera with their mouse whilst a “Free Look” button was held, but didn’t get quite the right effect.

THIS is more what I’m trying to achieve. So I was wondering if anyone could suggest a way to sort of attach the camera to a rail, which mouse input can move it along.

Well, it’s hard to tell from the video, but it looks like the whole camera movement as well as rotation is predefined in a “one dimensional slide”. The easiest way to implement that in Unity is to use a simple AnimationClip. That way you can simply create an animation that moves from the left most “position” over to the right most “position”. Including the rotation.

An animation clip can have multiple curves for several different things. So you can define a seperate curve for the location of the camera as well as the rotation.

You don’t simply “play” the animation but instead you set the current “time” of the animation and sample the animation manually. That time value is what you put under control of the user. As normalized time it’s just a value between 0 and 1.

Of course it would also be possible to only animate the movement and rotation around “y” and let the user still control the “up-down axis” (local x). So for example the mouse x delta would drive the animation position and the mouse y delta would rotate the camera up down.

edit

I made a quick WebGL Demo. You have to click once to lock the mouse cursor. I’ve exported the project files as package.

Note that the AnimationClip need to be a legacy clip in order to use it in the Animation component. To turn an AnimationClip into a legacy clip you have to temporarily switch the inspector into debug mode. While in debug mode you will see the legacy checkbox. Once changed you can switch the inspector back to normal mode.