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 trialMukesh Ranjan
Courses Plus Student 4,075 PointsTime.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
Treehouse TeacherHi 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!
Mukesh Ranjan
Courses Plus Student 4,075 PointsHi 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.
Jennifer Nordell
Treehouse TeacherUnfortunately, I don't know other than that one error in your code. Sorry.
Yousef Almutairi
271 PointsHi, 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 :) .