# Camera Follow with SmoothDamp one axis jitter

Hello everyone! This is my first time building a game from scratch and I have a few questions.

The game I’m working on is a rail shooter and the issue is related to the CameraFollow script.

My problem: I have a camera that follows the player, but without leaving a predefined area (5x5). The player position is also clamped by using ‘Camera.WorldToViewportPoint’ and ‘Camera.ViewportToWorldPoint’.

I use SmoothDamp, but for some reason when the player moves in the Y-axis the Camera follows it with jitter.

Code:

``````public class CameraFollow : MonoBehaviour
{
public Vector3 velocity= Vector3.zero;
// some other variables

void Update()
{
FollowTarget(target);
}

void LateUpdate()
{
Vector3 localPos = transform.localPosition;
transform.localPosition = new Vector3(Mathf.Clamp(localPos.x, -limits.x, limits.x), Mathf.Clamp(localPos.y, -limits.y, limits.y), localPos.z);
}

public void FollowTarget(Transform t)
{
Vector3 localPos = transform.localPosition;
Vector3 targetLocalPos = t.transform.position;
transform.localPosition = Vector3.SmoothDamp(localPos, new Vector3(targetLocalPos.x + offset.x, targetLocalPos.y + offset.y, targetLocalPos.z + offset.z), ref velocity, smoothTime);
}

}
``````

I tried changing the velocity and even placing the FollowTarget function inside LateUpdate and FixedUpdate (which I’m still learning) but with no success.

My questions:

1 - What could be the reason behind that movement with jitter only in Y-axis? Could it be related to the screen size (mine is 1920x1080) ?

2 - Another thing that I noticed is that when I move the player to the upper-right corner, velocity value in X is greater that Y. Why is that?

3 - As regards smoothTime, I cannot pick a value greater that 0.125 without worsen the situation (for both axis). Am I missing anything else? maybe related to the 2 last parameters in SmoothDamp

I don’t think you should use Update() for smooth damp. It is typically used in LateUpdate() after all other calculations are computed. Right now you are moving the camera in Update and then doing your clamping before rendering in LateUpdate. Also, if the player is already clamped, is it necessary to have the Camera clamped??

Instead of having an offset vector, you may want to look at just having an empty game object as a child of your player that is placed at the offset position then just use that empty as the camera target for the smooth damp.If you need to adjust the empty target position to clamp it in some way, just move that target and the camera will follow via the smooth damp which I’m sure will help with jitter.

Also, you are not calculating the camera smooth damp properly. You are passing in the target transforms world position as the target smooth damp position, but you are trying to update the local position of the camera. Since you are passing the world position of the target, you should work with the world position of the camera as well.

``````[SerializeField] Transform target; //Empty transform placed as child on player at offset

Vector3 velocity = Vector3.zero;
[SerializeField] Vector2 limits;

float smoothTime = 0.4f;

void LateUpdate()
{
if(Mathf.Abs(target.position.x) > limits.x || Mathf.Abs(target.position.y) > limits.y)
{
target.position = new Vector2( Mathf.Clamp(target.position.x, -limits.x, limits.x), Mathf.Clamp(target.position.y, -limits.y, limits.y);
}
transform.position = Vector3.SmoothDamp(transform.position, target.position, ref velocity, smoothTime);
}
``````

That should work for your situation

Thanks for answering. This is what I’m trying to achieve:

As you can see, the camera cannot leave the square, but the player has a wider area of movement. That’s why I’m clamping player’s position with ‘Camera.WorldToViewportPoint’ and ‘Camera.ViewportToWorldPoint’ (inside Update()).

I also tried moving ‘FollowTarget’ method of ‘CameraFollow’ script to LateUpdate() but it made everything worse (jitter all the time)

Please forget about the offset. In my case, I only modified the Z-axis because I want the camera to be 5 units behind the player.