Mathf Clamping Issues with variables

Hi,

I have a line of code that’s not working. I’m clamping the transform x. Whenever I replace m_Clamping with a fix number like -3.28 and 3.28. The code works but if I use a variable it fails. I feel I’m doing it right but somehow if the m_Clamping variable is passed a float it still does not translate.

Vivienne

private float m_Clamping = 0.0f;


private void FixedUpdate()
    {

        // Get Position
        Vector2 newPosition = transform.position;

        newPosition.x = Mathf.Clamp((float)newPosition.x, (float)-m_Clamping, (float)m_Clamping);

        transform.position = newPosition;
    }


public void SetClamping(float clamp)
    {
        m_Clamping = clamp;
    }

What are the results you’re seeing? What’s the value of m_Clamping, and what are you getting after the Clamp operation?

Is it possible you’re not calling SetClamping, so m_Clamping initializes with 0?

Are you sure m_Clamping is being set to the value you want? I’d try adding a Debug.Log statement somewhere to verify its value.