3D Pong Clone: Set position of a Rigidbody, or change how a speed vector is verified?

Hi there!

I am experimenting with mouse input as a way for me to learn more on what Unity can do, as I’m interested in learning on how to build games with it. Simply put, I’m a newbie to video game development.

There’s one problem I’m having, and that is if I move my mouse up or down too fast, my player paddle (colored red) in my 3D Pong clone goes out of bounds. By that, I mean, rather than being stuck between two light green walls, as you see below:


the paddle goes out of the space and thus the player can’t bounce the ball back towards the opponent.

I tried to solve this on my own using range limits, overriding the Start function in the Player script (which inherits from the Paddle class, which in turn inherits from MonoBehaviour), but the only thing I really got was getting my red paddle stuck right at the top wall when I move too fast upwards.

Here’s the code I got so far for my Player paddle (for brevity, I’m showing you my FixedUpdate function for now. Please let me know if you need to see more):

    // Update is called once per frame
    // Instead of floating-point values, movement should be controlled by a Vector3 object.
	void FixedUpdate () 
		if (GameController.isTheGameOn())
            float moveUpOrDown = 0.0f;

            // Get input from Player 1's input on keyboard
            if (typeOfPlayer == PlayerNumber.PLAYER_ONE && typeOfInput == InputType.KEYBOARD)
                moveUpOrDown = Input.GetAxis("Player 1 Keyboard Input");
            // Get input from Player 2's input on keyboard
            else if (typeOfPlayer == PlayerNumber.PLAYER_TWO && typeOfInput == InputType.KEYBOARD)
                moveUpOrDown = Input.GetAxis("Player 2 Keyboard Input");
            // Get input from the mouse (up or down)
            else if (typeOfInput == InputType.MOUSE)
                moveUpOrDown = Input.GetAxis("Mouse Y");
            // Get input from the joystick
                moveUpOrDown = Input.GetAxis("Vertical Joystick");

            speedVector = new Vector3(0.0f, moveUpOrDown * Time.deltaTime * 2.5f, 0.0f);

            /* Problem with the mouse input: The paddle can go out of bounds in the game. */
            // This is where I initially had my boundary conditions checked for the speed vector,
            // and where I introduced the bug that the paddle gets stuck at the top wall and can't 
            // move from mouse input.
            if (speedVector.y + board.transform.position.z >= maxYPosition
                || speedVector.y - board.transform.position.z <= minYPosition)
                speedVector = Vector3.zero;

            // Translate is used here instead of setting the position explicitly, primarily 
            // for performance.

Right now, at optimal performance, the game runs at 50 frames per second. With mouse input, there definitely has to be strict limits such that even a strong flick won’t make the paddle go out of bounds. I am thinking of either using board.MovePosition or set the position within my boundary check, or that I research myself and find other ways to limit my speed vector from my input.

I do remember coming across a question somewhere in this forum where someone suggested to use Mathf.Clamp and predict what the next transform position would be, but I can’t remember what the question is from the top of my head.

Any suggestions?

Mathf.Clamp is exactly what you’d want to use. It’s covered in more detail in the “Space Shooter” tutorial, here.
Since you’ve already got your min / max Y positions, the code would look something like:

 Mathf.Clamp (rigidbody.position.y, minYPosition, maxYPosition)

I don’t claim this is the exact code to use, but it should help as a starting point. I recommend following along with the linked tutorial, at least that section, since it explains the concepts in greater detail.