- Home /
Smooth Camera Vertical Follow
Hello All,
I've got what I thought was quite a simple camera follow script. But alas it's not working perfectly.
The plan is to follow my hero (an asteroid), smoothly downwards as it travels downwards. The camera only goes downwards (there's not turning around) and only travels down the Y axis.
I thought I'd simply check how far past the centre the hero was and start panning downwards relatively. SImple. As the camera moves down it catches up with the hero and therefore creates its own ease.
At first it seems to be working fine, but once things get fast, or once I increase the modifier (which basically works to set the speed at which it catches up), it starts to jitter. This jitter gets worse the quicker you're going.
Is there a better way to do this? Or is there something obviously wrong with my code?
void LateUpdate () {
Vector3 targetPos = camera.WorldToViewportPoint(target.transform.position);
// Calculate distance from centre and affect Transform
float velocity = (targetPos.y - 0.5f) * modifier;
velocity = Mathf.Min(0, velocity);
transform.Translate(0, Time.deltaTime*velocity, 0);
}
It seems like you're seeing the effect of bigger increments of space being traveled by your camera between each frame. You might want to try making your camera a child of your player and modifying the script accordingly.
Thanks. I'll try this...
Trying to get my head around how to keep a sense of movement, I guess I move my player downwards based on speed. I'll have a go and report back.
Hmm... so now I've got a camera rotating around the player like a maniac. How do I counteract this elegantly? Tried shielding it in a locked rigid body but that creates new problems.... in fact, should this be a new question?
Did you change your code to reflect the fact that the new coordinates are going to be locally based (relative to your player)? Using Space.World as the last parameter in your transform.Translate would counter that, but it may also cost you whatever benefit you might get from making the camera a child of your player in the first place.
Answer by robertbu · Jul 10, 2013 at 05:44 AM
I don't know if this is any less jittery, but here is an alternate:
public class TrackOnY : MonoBehaviour {
public Transform target;
public float speed = 1.0f;
void LateUpdate () {
Vector3 v3 = transform.position;
v3.y = Mathf.Lerp (v3.y, target.position.y, Time.deltaTime * speed);
transform.position = v3;
}
}
Hey Robert... no luck unfortunately... still getting the Jitter, but thank you... it seems a neater, deltaTime orientated option, so I'll keep it.
Your answer
Follow this Question
Related Questions
Cinemachine Confiner 2D shift my camera view 0 Answers
Smooth camera script looking at left hand side of character. 0 Answers
How to point a camera at a point in front of player. 1 Answer
Resetting Camera with a key input 1 Answer
Why doesn't my camera stay on the moving Waypointprogresstracker circuit? 0 Answers