Hi everyone !
I’m a newbie here and with Unity.
i try to build my own platformer game, in 2.5D (let’s say in the way of Inside, Antother world…).
my player (in 3D in 3D world) can only move on the X axis (walk, run, crouch…).
I would like the Camera moves on the Z axis when the player is running, like a zoom out. But i prefer not use camera depth and really move the camera.
and i want the movement to be progressive and smooth.
For instance : let’s say the camera is at 0 on the Z axis by default, when the caracter starts to run, the camera goes from 0 to 150 on the Z Axis in 0,5 seconds, stay at 150 as long as he runs, and come back to 0 as he stops…
I don’t know how to proceed… did a lot of tests but i don’t understand enough code to achieve that… maybe someone could help me a bit here ?
i already have a controller script for my camera (cameraFollow)
and a controller for my player.
I tried to ad an “Animator component” on my camera, with three “motions”, CameraIddle (at 0) CameraZoomIn (0 to 150 in 0,5 sec) and CameraZoomOut (150 to 0 in 0,5 sec) and to associate to the Running key first in the Player controller script, thant in camera controller script, but didn’t work.
i tried several things that i barely understood, found on forums but nothing worked…
Thanks !
I did this in my Impulse One game: I mapped camera Z to speed in the same way (I think) you want.
Here was what I did:
My camera controller has:
-
2 preset distances: near and far. These are set up for a given level.
-
2 preset player velocities, min and max, also set up for level
The above min/max velocities are mapped to the Z distance, as follows:
Each frame the camera controller reads the player’s velocity magnitude.
-
First it uses Mathf.InverseLerp(minSpeed, maxSpeed, currSpeed)
to end up with 0 to 1 speed fraction
-
Now feed that speed fraction into Mathf.Lerp( nearZ, farZ, speedFraction)
to get a desired Z
If you feed that Z straight into the camera it is jerky, especially if you accelerate really hard or stop suddenly.
Instead, do a time filtered tween, keeping another “current Z” and using some fraction to lerp towards it smoothly, something like this:
float currentZ; // member variable
and in your camera update:
// how we get desiredZ, as described above
float desiredZ = Mathf.Lerp( nearZ, farZ, speedFraction);
const float Snappiness = 2.0f; // how quickly the camera pulls back/forward
// this filters it to be smoother
currentZ = Mathf.Lerp( currentZ, desiredZ, Snappiness * Time.deltaTime);
// now use currentZ to position your camera's Z position
That should get you a long way towards what you’re looking for. Fiddle with the numbers to change the characteristics of it.
Thanks a lot Kurt !
your solution seems really better.
i’m a very beginner in code, so i’m sorry to ask but how do you assign these presets for the distance “near” and “far” ? and for min & max velocity as well ?
thanks !
1 Like
For the first cut you can just put them right in the code as constants:
const float minZ = 10;
const float maxZ = 20;
etc.
Then get everything plumbed so that at least it works a bit, and then you can go back and either tweak it in code, OR… the Unity way is to make those values public so you can tweak them in the inspector.
public float minZ;
public float maxZ;
For extra bonus points, add a Reset() function like so:
void Reset()
{
minZ = 10;
maxZ = 10;
}
And that is the reference way to set up presets in Unity-land.
Having a Reset() function lets you right-click on the hamburger in the upper right corner of the inspector panel for this script and reset those values to know values. Plus Reset() is called when the Monobehavior is added.
I would also recommend you to take a look to Unity’s Cinemachine. It’s easy to use and very powerful when you seek for a more cinematographic camera 
2 Likes
Thanks you both of you !
i’ll try your method kurt but it’s a little bit hard for me since i don’t understand code even if i try !
i’lll have a look with cinemachine Giusttita, looks great !