- Home /
2D Camera Smooth follow, FixedUpdate and LateUpdate odd difference, help needed.
Hello my camera follow script is : using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CamSmoothFollow : MonoBehaviour
{
public float smoothTimeY, smoothTimeX;
Vector2 velocity;
public GameObject karakter;
public float offsetX, offsetY;
private void FixedUpdate()
{
float posX = Mathf.SmoothDamp(transform.position.x, karakter.transform.position.x, ref velocity.x, smoothTimeX);
float posY = Mathf.SmoothDamp(transform.position.y, karakter.transform.position.y, ref velocity.y, smoothTimeY);
transform.position = new Vector3(posX+offsetX, posY+offsetY, -10);
}
}
As you can understand, gameobject "karakter" is our player that camera should follow. In this script, camera is not smooth correctly. I saw that i needed to use LateUpdate instead of FixedUpdate. I changed it and camera just gone crazy. I mean it was always in a loop in that situation. I only changed Fixed to Late why is this happening? And how can i make the camera movement smooth as i wanted in LateUpdate?
Answer by denizhd21 · Oct 25, 2021 at 04:42 PM
Update: I just changed the script to: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CamSmoothFollow : MonoBehaviour
{
public float smoothTimeY, smoothTimeX;
Vector2 velocity;
public GameObject karakter;
public Vector3 offset;
public Vector3 aa= Vector3.zero;
private void FixedUpdate()
{
// float posX = Mathf.SmoothDamp(transform.position.x, karakter.transform.position.x, ref velocity.x, smoothTimeX);
//float posY = Mathf.SmoothDamp(transform.position.y, karakter.transform.position.y, ref velocity.y, smoothTimeY);
Vector3 desiredPos = karakter.transform.position + offset;
Vector3 smoothPos =Vector3.SmoothDamp(transform.position, desiredPos, ref aa, smoothTimeX);
transform.position = smoothPos;
}
}
I made previous codes comment line so anyways, in this case FixedUpdate worked better than LateUpdate. Actually LateUpdate was incredibly NOT smooth at all! FixedUpdate is way smooth in this but not fully, am I doing something wrong?
Your answer
Follow this Question
Related Questions
Main Camera child under player, causes flip 3 Answers
Understanding smoothTime in Mathf.SmoothDamp 0 Answers
Smooth camera shift, Lerp? SmoothShift? 2 Answers
(2D) Camera negative boundaries 0 Answers
Smooth Camera 2D Follow Player 0 Answers