(Solved) Stop shaking of a gun?

I have this peace of code, which is supposed to make your gun follow your main camera to achieve smooth movement. (So the gun doesn’t have to be a child of the camera)

var PlayerTransform : Transform;

var RotationSpeed : float;

var PlayerCamera : GameObject;
var PlayerGameObj : GameObject;

//Targets

@HideInInspector
var TargetXrotation : float;

@HideInInspector
var TargetYrotation : float;

@HideInInspector
var TargetXrotationV : float;

@HideInInspector
var TargetYrotationV : float;

function Update () {

	transform.position = PlayerCamera.transform.position + (Quaternion.Euler(0,TargetYrotation,0) * Quaternion.Euler(TargetXrotation,TargetYrotation,0) * Vector3(0,0,0));

	TargetXrotation = Mathf.SmoothDamp(TargetXrotation, -PlayerCamera.GetComponent(MouseLook).rotationY, TargetXrotationV, RotationSpeed);
	TargetYrotation = Mathf.SmoothDamp(TargetYrotation, PlayerCamera.GetComponent(MouseLook).RotationX, TargetYrotationV, RotationSpeed);
	
	transform.rotation = Quaternion.Euler(TargetXrotation, TargetYrotation, 0);
	
}

I don’t fully understand the code (followed a tutorial), but I do get most of the logic.
I wanted the rotation speed variable to be 0.01, because I wanted the gun to almost directly follow the camera.

My problem is, my gun starts shaking when I move, and it also moves forward when I walk forward.
I don’t want that, but I can’t find a way to stop it. Does anyone know how?

(I think it could also be something with the settings of the first person controller and the main camera. (the mouselook script setting on those objects) I adjusted them a bit and don’t know if I did the right thing.
(I made FPController MouseX and Main camera MouseY, sensetivity 15 everywhere but I don’t know the right max and min values.)

Try changing the Update to FixedUpdate, on the script, that you are showing here…

Putting in the LateUpdate() worked fine to me;