Question by
S_Byrnes · Apr 14, 2019 at 05:22 AM ·
c#playercamera movementjitter
Why Does My Camera Jitter When Following Player?
Hi there,
I'm attempting to create a SmoothDamp camera follow that has a dead zone around the center of the screen, when my player moves out of this dead zone the camera should smoothly follow the player until he is back in the dead zone again.
My issue is the player appears to jitter back and fourth when the camera begins to follow, I'm hoping someone here can help me out, I've posted my code below, please let me know if there is anything you can see that I need to fix?
public class CameraMovement : MonoBehaviour
{
public Transform playerLocation;
private Vector3 moveTemp;
public float yDifference;
private float movementThreshold = 70f;
public float smoothDampTime = 0.5f;
private Vector3 smoothDampVelocity = Vector3.zero;
void LateUpdate()
{
if (playerLocation.transform.position.y > transform.position.y)
{
yDifference = playerLocation.transform.position.y - transform.position.y;
}
else
{
yDifference = transform.position.y - playerLocation.transform.position.y;
}
if (yDifference >= movementThreshold)
{
moveTemp.y = playerLocation.transform.position.y;
moveTemp.z = -10;
moveTemp.x = 0;
float y = Mathf.SmoothDamp(transform.position.y, moveTemp.y, ref smoothDampVelocity.y, smoothDampTime);
transform.position = new Vector3(transform.position.x, y, transform.position.z);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612195404im_/https://answers.unity.com/themes/thub/images/avi.jpg)