How to make an object move between a certain range and have that range to be taken into account not with a transform.position value but with the object's dimensions?

I have a cube. That cube is moved by a script that alters the transform.position. Colliders dont work on it it just goes straight through them. I need a way of limiting the movement of that object. Here is the script that does that part:

Vector3 tp = transform.position;
tp.z = Mathf.Clamp(tp.z, -3.25f, 3.25f);
transform.position = tp;

The problem with this script is that the size of my cube changes (you cut it, you may cut it in half or 1/4th, 1/7th etc.) which means that by getting the transform.position you only limit its movement by the transform, not the shape of it. This means that when it’s beside a wall it stops at certain coordinates instead of when the mesh itself is touching the wall. Would appreciate some help i have been searching for an answer for well over 2 hours without a break.

I should also add that I can’t use OnTrigger because for some reason it messes with other scripts in my project.

If your cube always stays as a cube, doesn’t rotate, only changes size and you can access the current dimensions, you can just do something like this:

//init
float upperLimit;
float lowerLimit;
float originalSize;

//update
float offsetZ = currentDimensions.z / 2
tp.z = Mathf.Clamp(tp.z, lowerLimit + offsetZ, upperLimit - offsetZ);

Otherwise, you might need a rigid body.