I’m need a camera that follows the player on the Y axis in an elastic fashion while always keeping him inside the screen frame.
I’ve spent a lot of time trying out different variations of code, but they always give me a jerky, stuttering effect when the player moves fast.
In my game, the player can jumps very high, achieving large acceleration. I need the camera to always keep the player in frame. On the other hand, when the player’s movements are small, I want the camera to be fairly elastic and give him freedom to move around the screen. Here’s what I got:
void LateUpdate () {
float dist = transform.position.y - playerObject.transform.position.y;
springiness = Utilities.EaseInCubic(2.0f, 40.0f, dist / (18.0f));
if (springiness > 40.0f) {
springiness = 40.0f;
}
Vector3 targetPosition = new Vector3(transform.position.x,playerObject.transform.position.y,transform.position.z);
transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * springiness);
}
I’m using a custom EaseInCubic function to jack up the damping value (“springiness”) when the camera’s distance from the player is greater than 18 units (which is about half the screen height). This sorta works, the player is always kept in-frame, but when he moves fast, everything jitters pretty badly.
What’s a better way to do this? I have a suspicious that using Lerp here is causing the problem. Are they any better ways of updating the camera’s position?