[Solved] Section 5 Lecture 74 - Paddlepos.x doesn't get clamped

It would appear that the paddle position gets set to either my min x or max x depending on which order I have them in, instead of being clamped within the play space. I am using Unity 5.5

Here is the code I am using

   namespace Block_Breaker
    public class PaddleController : MonoBehaviour
        // Variables - Define Paddle Control
        public float paddleSpeed;
        private Rigidbody2D rb2D;
        private Vector2 vel;


        // Use this for initialization
        void Start()
            rb2D = GetComponent<Rigidbody2D>();

        // Update is called once per frame
        void Update()

        void PaddleMovement()
            Vector3 paddlePos = new Vector3(0.5f, this.transform.position.y, 0f);

            float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16; ;

            paddlePos.x = Mathf.Clamp(mousePosInBlocks, 3.5f, -3.5f);
            this.transform.position = paddlePos;


I am having trouble understanding what the problem is? From looking at the code there might be a problem declaring and initializing mousePosInBlocks within the function - instead of being class wide, repeat might.

But there definitely a problem with the clamping - I don’t know if you’ve done that for testing purposes but you’ve got those values backward. The first value should be around 0.6f (for the left hand edge of the screen) and the second value should be around 15.4 (for the right hand side). Having a negative value would be offscreen. And clampf is used with the minimum value first then the maximum.

From the code I have it as this:

  • paddlePos.x = Mathf.Clamp (mousePosInBlocks,0.7f, 15.3f);

But then again my bat is a lot longer for testing purposes. Try changing it to something along those lines and see if it works.

Put the -3.5f before the 3.5f in the method call. Smaller goes before larger in Mathf.Clamp().

1 Like

Privacy & Terms