'Rotating around Z axis

I am new to unity and C#, I have been trying to make the object tilt to the right or left when it is moving but what I have is that the object is rotating in a full circle around the Z axis.

that's what I got, I had it from the unity documentation.

    [SerializeField] private float _moveSpeed = 50f;
    [SerializeField] private float _turnSpeed = 3f;
    public float rotationSpeed = 100.0f;

    // Start is called before the first frame update


    // Update is called once per frame
    void Update()
    {
     
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            transform.position += Time.deltaTime * _moveSpeed * transform.right;

            float translation = Input.GetAxis("Vertical") * _moveSpeed;

            float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
            translation *= Time.deltaTime;
            rotation *= Time.deltaTime;
            transform.Translate(0, 0, translation);

            transform.Rotate(0, 0, rotation);


        }

that's what I got, I had it from the unity documentation.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source