The camera rotates and makes the object follow the rotation

Hi.
Depending on the script and the actual game, the object can follow the rotation when the camera rotates along the y-axis, but the object does not follow the rotation when it rotates along the x-axis.Why?

public class handObjectMove : MonoBehaviour
{
    public Transform target;

    [SerializeField]
    private float followSpeed = 12f;

    private void Update()
    {
        if (target != null)
        {
            Vector3 targetPosition = target.position;

            transform.position = Vector3.Lerp(transform.position, targetPosition, followSpeed * Time.deltaTime);

            transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, followSpeed * Time.deltaTime);
        }
    }
}

Thanks for your help!

image
I added this script to HandObject, and the “target” is MainCamera

original code

public class handObjectMove : MonoBehaviour
{
	public GameObject target;
	
	private Vector3 vectOffset;

	[SerializeField] private float followSpeed;
	
	private void Start()
	{
		vectOffset = transform.position - target.transform.position;
	}

	private void Update()
	{
		transform.position = target.transform.position + vectOffset;
	    transform.rotation = Quaternion.Slerp(transform.rotation, target.transform.rotation, followSpeed * Time.deltaTime);
	}
}

Pic

Your script is not keeping the item in front of the camera. To hold an item in front of the camera:

private void Update ()
{
	if (target != null) {

		//Hold item in front of camera
		const float DistInFrontOfCamera = 1.0f;
		transform.position = target.position + (target.forward * DistInFrontOfCamera);

		//Have item face the same direction as camera
		transform.rotation = target.rotation;

		//Optional: adjust item Y rotation as neccessary
		const float YRotationAdjustment = 180f;
		transform.Rotate (new Vector3 (0f, YRotationAdjustment, 0f));
	}
}