Animating the Camera
To achieve what you want, you need to animate the camera position. You’ll find next a code example and explanation of this code.
What the code do
In this code, we assume that the camera is a children of the character that you are following.
When we call the StartLag function we’re telling the code to start animating an offset on the camera position. The animation will take lagDuration
(in this example, 2 seconde) to play the lag. The camara will be move base on the offset
vector (-5 unit back on the X axis).
SmoothStep
For animating, we use the Mathf.SmoothStep
function. Here the description:
This function interpolates between min and max in a similar way to Lerp. However, the interpolation will gradually speed up from the start and slow down toward the end. This is useful for creating natural-looking animation, fading and other transitions.
We’ll use that function 2 time:
- FROM original position TO offset position
- FROM offset position TO original position
lagPeak
You’ll probably want to reach the offset position quicker than it take to come back to the original position. lagPeak
allow you that. To calculate the time it take to reach the peak, multiply lagPeak
and lagDuration
. Here an example of what it mean:
- If
lagDuration == 2f
and lagPeak == 0.5f
, the camera will reach is offset in 1 second and come back in 1 second.
- If
lagDuration == 2f
and lagPeak == 0.1f
, the camera will reach is offset in 0.2 second and come back in 1.8 second.
Code Example
//Public Interface. Tweak these value to change the result.
[Range (0,1)] public float lagPeak = 0.5f;
public float lagDuration = 2.0f;
public Vector3 offset = new Vector3(-5,0,0);
//Internal variable (not to modify)
private float movementTimeStart = 0.0f;
private bool isLagin = false
///Initialize Lag animation value
void StartLag()
{
isLagin = true;
movementTimeStart = Time.time;
Vector3 camInitialPosition = this.transform.localPosition
Vector3 camOffsetPosition = camInitialPosition - offset;
}
void Update()
{
AnimateLag();
}
///Animate the Lag
void AnimateLag()
{
float t;
//If the animation duration is invalid, jump to the end. (Prevent dividing by 0)
if (lagDuration <= 0.0f)
{
t = 1;
}
else
{
//Calculate the pourcentage of the animation
t = (Time.time - movementTimeStart) / lagDuration;
}
//Animate the piece
//
// The animation is separete in 2 part, Reaching the peak and comming back.
// To adjust the peak timing, we use the lagPeak variable that represent when (in %) the peak should be reach
if(t < lagPeak)
{
this.transform.localPosition = new Vector3 (Mathf.SmoothStep (camInitialPosition.x, camOffsetPosition.x, (t * (1/lagPeak))),
Mathf.SmoothStep (camInitialPosition.y, camOffsetPosition.y, (t * (1/lagPeak))),
Mathf.SmoothStep (camInitialPosition.z, camOffsetPosition.z, (t * (1/lagPeak)));
}
else
{
this.transform.localPosition = new Vector3 (Mathf.SmoothStep (camOffsetPosition.x, camInitialPosition.x, (t * (1/(lagPeak-1)))),
Mathf.SmoothStep (camOffsetPosition.y, camInitialPosition.y, (t * (1/(lagPeak-1)))),
Mathf.SmoothStep (camOffsetPosition.z, camInitialPosition.z, (t * (1/(lagPeak-1))));
}
//If we have finish
if (t >= 1)
{
//Reset Animation data
isMoving = false;
movementTimeStart = 0.0f;
camInitialPosition = Vector2.zero;
camOffsetPosition = Vector2.zero;
lagDuration = 0.0f;
}
}
For more code explaination, just comment.