Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Player Input and Cameras Make a Follow Camera

Mukesh Ranjan
PLUS
Mukesh Ranjan
Courses Plus Student 4,075 Points

Time.deltaTime keeps changing even though frog id not moving

When I click on play in the game editor camera keeps on changing position like shaking up and down. I put the logger and saw that Time.deltaTime is changing even though frog is not moving so this becomes different every frame : cameraFollowSpeed*Time.deltaTime. When I remove the Time.deltaTime then it is OK but effect of camera lazy following the frog is not there. Any idea what must be wrong ?

Following is my code :

public class FollowCamera : MonoBehaviour {

[SerializeField]
private Transform player;

[SerializeField]
private Vector3 offset;
private float cameraFollowSpeed = 5f;



// Update is called once per frame
void LateUpdate () {
    Debug.Log (Time.deltaTime);
    Vector3 newPosition = player.position + offset;
    transform.position = Vector3.Lerp (player.position, newPosition, cameraFollowSpeed*Time.deltaTime);

}

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're multiplying cameraFollowSpeed by Time.deltaTime. These should be added instead:

transform.position = Vector3.Lerp (transform.position, newPosition, cameraFollowSpeed + Time.deltaTime);

Make this change and see how it looks! :sparkles:

Mukesh Ranjan
PLUS
Mukesh Ranjan
Courses Plus Student 4,075 Points

Hi Jennifer. Thank you for your answer. By adding instead of multiplying Time.deltaTime the effect is same as not adding the Time.deltaTime and just using cameraFollowSpeed. The camera and player move and stop at same time. In the lecture video the effect is that of lazy following by camera.Player moves first and then camera follows.

Yousef Almutairi
Yousef Almutairi
271 Points

Hi, You can solve the problem by replacing (player.position) to (Transform.position) as shown below:

transform.position = Vector3.Lerp (transform.position, newPosition, cameraFollowSpeed*Time.deltaTime);

because you are telling the unity to follow the frog at the camera position, not from player object position. Hopefully you got it :) .