How to stop my obstacle sinking and floating and follow the rotation of my stage?

I am just making the game that rotating stage and move the ball into a goal, in that game, I made a obstacle going forward and backward, but when I run the game and rotate my stage, (I think) due to the Vector 3 issue, My obstacle is sinking or floating when I rotate my stage. I want to know any solutions for this or how to solve this problem to fix the distance between stage and obstacle as first distance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RepeatMovementOfShark : MonoBehaviour
{
    public float zSpeed = -1.0f; //units per second, set negative to go the other way
    public float zDistance = 10.0f; //distance to travel before turning back
    public bool FreezeY = false;
    private Vector3 startPos;
    float way = 1.0f;
    float zPos = 0.0f;
    float zPosPrev = 0.0f;
    void Start()
    {
        //save start position
        startPos = transform.position;
    }
    void Update()
    {
        Vector3 currentPos = transform.position;
        //advance position
        zPosPrev = zPos;
        zPos += way * zSpeed * Time.deltaTime;
        //handle turn around at end
        if (Mathf.Abs(zPos) > zDistance)
        {
            way = -way;
            zPos = zPosPrev;
        }
        //handle turn around at start
        if ((zPos < 0.0f && zPosPrev > 0.0f) || (zPosPrev < 0.0f && zPos > 0.0f))
        {
            way = -way;
            zPos = zPosPrev;
        }
        if (FreezeY)
        {
            currentPos.y = startPos.y;
        }
        //set new position
        transform.position = new Vector3(transform.position.x, transform.position.y, startPos.z + zPos);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Transform.rotation example.
// Rotate a GameObject using a Quaternion.
// Tilt the cube using the arrow keys. When the arrow keys are released
// the cube will be rotated back to the center using Slerp.
public class StageRotation : MonoBehaviour
{
    public float smooth = 5.0f;
    public float tiltAngle = 60.0f;
    void Update()
    {
        // Smoothly tilts a transform towards a target rotation.
        float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle * -1;
        float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle * -1;
        // Rotate the cube by converting the angles into a quaternion.
        Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
        // Dampen towards the target rotation
        transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
    }
}

You’re changing the Transform and should try to understand what that actually means. In terms of physics it means when you change it (which is per-frame), during the next simulation step (which isn’t by default per-frame) it has to then instantly reposition the Rigidbody to your Transform pose and that probably also causes overlaps of colliders which the sovler then suddenly has to try to solve over time (multiple simulation steps) but then you continue to change it per-frame.

When you add a Rigidbody it’s because you want IT to control the Transform, not the other way around. If you’ve not added a Rigidbody it’s because you are implicitly stating it’s Static i.e. not moving so don’t go changing the Transform. Can you change the Transform? Yes. Can you do it without it causing issues? No.

In short, you should NOT be modifying the Transform. You should move things via the Rigidbody API itself. For stuff that moves under your control only, the body should be Kinematic. For Kinematic bodies you should use Rigidbody.MovePosition/MoveRotation. These work with physics and also support interpolation which provides smooth motion due to the fact that physics isn’t running per-frame by default.

Moving physics stuff per-frame is pointless and wasteful in most cases if the physics is only updating during the fixed-update. If fixed-update is 50hz and you’re updating it per-frame at (say) 200hz then you change it 4 times before any physics update. If you want to do this per-frame then do manual simulation per-frame but that’s another story.

Thank you for replying my post.
I almost forgot to notice one information that I already import the obstacle object as child of stage. I am sorry that I forgot to notice important information