Player's Body goes half way through the screen in the x axis?

So I been currently trying to restrict the player’s movement dynamically without hard coding any values.
As of now, this how my player controller class looks like.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public const float MAX_SPEED = 5.0f;

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Input.GetAxis("Horizontal") * MAX_SPEED * Time.deltaTime, 0.0f, 0.0f);
        clampPlayersMovementX();
    }

    void clampPlayersMovementX()
    {
        Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);
        pos.x = Mathf.Clamp01(pos.x);
        transform.position = Camera.main.ViewportToWorldPoint(pos);
    }
}

After a reading a couple of posts in the unity forums, the general consensus is to use Mathf.clamp but the problem is that my clampPlayersMovementX function still lets half of the players body go through the screen and the reason why is because the object’s pivot is at the center. So I’ve tried to use bound.center.x property in the sprite renderer component to see if it would completely stop the player from going off the screen but it did not help. Any ideas?

Use the sprite.pivot property or the sprite.bounds.extents:

Vector2 spriteCenter = sprite.pivot / sprite.pixelsPerUnit;
//or
Vector3 spriteCenter = sprite.bounds.extents;

If you want to support sprites with non-center pivots you can do:

//Units on the left from the sprite's pivot.
float xLeft= sprite.pivot.x / sprite.pixelsPerUnit;

//Units on the right from the sprite's pivot.
float xRight = (sprite.texture.width - sprite.pivot.x) / sprite.pixelsPerUnit;

Then you have to check two x values instead of one in the clampPlayersMovementX method:

Vector3 posMin = transform.position; //Sprite's left bound.
Vector3 posMax = transform.position; //Sprite's right bound.

posMin.x = posMin.x - xLeft;
posMax.x = posMax.x + xRight;

//Check if posMin.X and posMax.x are in the viewport.

This is what I was able to extrapolate from your comment but now the character goes completely pass the screen.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public const float MAX_SPEED = 5.0f;

    private float xLeft;
    private float xRight;
 
    void Start()
    {
        float pivotX = GetComponent<SpriteRenderer>().sprite.pivot.x;
        float pixelsPerunit = GetComponent<SpriteRenderer>().sprite.pixelsPerUnit;
        float textureWidth = GetComponent<SpriteRenderer>().sprite.texture.width;

        //Units on the left from the sprite's pivot.
        xLeft = pivotX / pixelsPerunit;
  
        //Units on the right from the sprite's pivot.
        xRight = (textureWidth - pivotX) / pixelsPerunit;
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Input.GetAxis("Horizontal") * MAX_SPEED * Time.deltaTime, 0.0f, 0.0f);
        clampPlayersMovement();
    }

   
void clampPlayersMovement()
    {
        Vector3 pos = transform.position;
        Vector3 posMin = transform.position;
        Vector3 posMax = transform.position;

        posMin.x = posMin.x - xLeft;
        posMax.x = posMax.x + xRight;

        pos = Camera.main.WorldToViewportPoint(pos);
        posMin = Camera.main.WorldToViewportPoint(posMin);
        posMax = Camera.main.WorldToViewportPoint(posMax);

        pos.x = Mathf.Clamp(pos.x, posMin.x, posMax.x);
        transform.position = Camera.main.ViewportToWorldPoint(pos);
    }
}

You are almost there. You can avoid doing so many transforms like so:

    private Vector3 minXPos = new Vector3(0, 0, 0);
    private Vector3 maxXPos = new Vector3(1, 0, 0);

    void clampPlayersMovementX() {
        float minX = Camera.main.ViewportToWorldPoint(minXPos).x;
        float maxX = Camera.main.ViewportToWorldPoint(maxXPos).x;

        Vector3 pos = transform.position;
        Vector3 posMin = transform.position; //Sprite's left bound.
        Vector3 posMax = transform.position; //Sprite's right bound.

        posMin.x = posMin.x - xLeft;
        posMax.x = posMax.x + xRight;

        if (posMin.x < minX) {
            pos.x = minX + xLeft;
        }
        else if(posMax.x > maxX) {
            pos.x = maxX - xRight;
        }

        transform.position = pos;
    }

Even better, if your camera is static you don’t have to do transforms at all. You can also skip the posMin and posMax vectors and do it like this:

using UnityEngine;

public class PlayerController : MonoBehaviour {
    public const float MAX_SPEED = 5.0f;

    private float xLeft;
    private float xRight;
    private Vector3 minXPos = new Vector3(0, 0, 0);
    private Vector3 maxXPos = new Vector3(1, 0, 0);
    private float minX;
    private float maxX;

    void Start() {
        float pivotX = GetComponent<SpriteRenderer>().sprite.pivot.x;
        float pixelsPerunit = GetComponent<SpriteRenderer>().sprite.pixelsPerUnit;
        float textureWidth = GetComponent<SpriteRenderer>().sprite.texture.width;

        //Units on the left from the sprite's pivot.
        xLeft = pivotX / pixelsPerunit;

        //Units on the right from the sprite's pivot.
        xRight = (textureWidth - pivotX) / pixelsPerunit;

        minX = Camera.main.ViewportToWorldPoint(minXPos).x;
        maxX = Camera.main.ViewportToWorldPoint(maxXPos).x;
    }

    // Update is called once per frame
    void Update() {
        transform.Translate(Input.GetAxis("Horizontal") * MAX_SPEED * Time.deltaTime, 0.0f, 0.0f);
        clampPlayersMovementX();

    }
   
    void clampPlayersMovementX() {
        Vector3 pos = transform.position;

        if (pos.x - xLeft < minX) {
            pos.x = minX + xLeft;
        }
        else if(pos.x + xRight > maxX) {
            pos.x = maxX - xRight;
        }

        transform.position = pos;
    }
}