- Home /
Camera shaking problem
Hi everyone, I am quite new in programming so I followed a YouTube tutorial on how to make the camera follow the player. Unfortunately my camera sometimes randomly starts to shake a bit, especially when the player is near bounds or colliders. Does any of you has an idea on how to fix this problem? Thanks in advance!
` public Transform lookAt;
public Vector3 offset = new Vector3(0, 5.0f, -10.0f);
void Start()
{
transform.position = lookAt.position + offset;
}
private void LateUpdate()
{
Vector3 desiredPosition = lookAt.position + offset;
transform.position = lookAt.position + offset;
desiredPosition.x = 0;
transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime);
}
}`
I solved this problem for my project the other day (Camera shaking when my player touched a wall), but my project is in 2D so I don't know if it'll work the same way for you. If your player has a RigidBody component, changing the Interpolation value might do something. I turned $$anonymous$$e to None and it fixed the problem, but other people said turning it to Interpolate fixed theirs.
Answer by RadonRaph · Dec 19, 2020 at 10:41 AM
Hello @arvidowic, You can try to smooth the movement, I do it like this personally: public Transform lookAt; public Vector3 offset = new Vector3(0, 5.0f, -10.0f);
void Start()
{
transform.position = lookAt.position + offset;
}
Vector3 desiredPosition;
float smoothAmount = 0.7f;
private void LateUpdate()
{
desiredPosition = Vector3.Lerp(lookAt.position + offset, desiredPosition, smoothAmount);
transform.position = lookAt.position + offset;
desiredPosition.x = 0;
transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime);
}
However you can trigger the movement only when distance is greater than a number like this: public Transform lookAt; public Vector3 offset = new Vector3(0, 5.0f, -10.0f);
void Start()
{
transform.position = lookAt.position + offset;
}
float minDistance = 2;
private void LateUpdate()
{
Vector3 desiredPosition = lookAt.position + offset;
transform.position = lookAt.position + offset;
desiredPosition.x = 0;
if (Vector3.Distance(desiredPosition, lookAt.position)>minDistance)
transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime);
}
I hope it will help you.
Your answer
Follow this Question
Related Questions
Camera shake 2 Answers
Camera points towards object but doesn't go to it 2 Answers
camera that follow the player ? 1 Answer
The base of every gameobject shake 1 Answer
Shaking camera 0 Answers