- Home /
Camera 2d following player with delay jumps
Hey, I was looking for answer for two days. Could not find it, or it just did not work as expected.
I need in my 2D game (X,Y) a camera that follow player. But the thing I need is that camera has some delays. Player starts moving and camera's waiting like 0.5 second (customizable) and starts follow in normal speed. + Player can run, then this delay should be a little bigger. And when player stops, camera normalize its position.
Well, I have this effect but.. while player walks, player is jumping likehow. You see something like little fps problem but it's not the issue. I've menage to find, that the problem is time parameter. But without time I have no idea how to make it. Using Vector3.Lerp, SmoothDamp, Slerp (don't know difference yet between them) and others.
method1(){
Vector3 point = camera.WorldToViewportPoint (playerTransform.position);
Vector3 delta = playerTransform.position - camera.ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, point.z)); //(new Vector3(0.5, 0.5, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp (transform.position, destination, ref velocity, dampTime);
}
method2(){
float newX = target.position.x;
float newY = target.position.y;
float z = transform.position.z;
float chaseSpeed = 0.05f;
transform.position = transform.position + (new Vector3 (newX, newY, z) - transform.position) * chaseSpeed;
}
method3(){
cameraTarget = new Vector3 (target.position.x, target.position.y, transform.position.z);
transform.position = Vector3.Lerp (transform.position, cameraTarget, Time.deltaTime * 4);
}
Three solutions and problems stays same. I've recroded a movie for the effect. https://www.youtube.com/watch?v=qEuiFIqAr3o and it's not a lag - while making cam as a child it follows player as it should, but without delay.
What I wish to know, how to make it smooth? Or alternative would be, making a box, that camera stays same and when player exit that box, then it would follow player. While the second approach I haven't tried to make yet. I really wish to have working first option
Answer by Sarseth · Jan 14, 2015 at 10:58 PM
Ok. I have the reason. It did not have anything with camera. At least not at all. I was moving rigid body instead of transform. Anyway, moving player was:
void Update () {
float x = Input.GetAxisRaw ("Horizontal");
float y = Input.GetAxisRaw ("Vertical");
Vector2 direction = new Vector2 (x, y).normalized;
rigidbody2D.velocity = direction * ((Input.GetButton ("Run")) ? runSpeed : walkSpeed);
}
Now I've changed it to
void Update () {
float x = Input.GetAxisRaw ("Horizontal");
float y = Input.GetAxisRaw ("Vertical");
Vector3 direction = new Vector3 (x, y, transform.position.z).normalized;
transform.position += direction * ((Input.GetButton ("Run")) ? runSpeed : walkSpeed) * Time.deltaTime;
}
And it stopped jumping ;) Don't know yet much about physic in unity - still learnig so maybe it's a known thing - but for me it was new. Posting for future ;) Thanks!
Your answer
Follow this Question
Related Questions
Smooth Camera 2D Follow Player 0 Answers
Why does the camera smoothly follow in one direction but not the other? 0 Answers
How to 2D Camera follow with lag. 1 Answer
Advice on implementing metroidvania camera 1 Answer
2D Camera Follow - Jittery? 1 Answer