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
else
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.
board.transform.Translate(speedVector);
}
}
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?
Answer by KieveKRS · Jul 03, 2016 at 08:55 PM
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.
Okay, I do have something that works, but I don't know if this is practical in a game coding sense.
This is what I got right now:
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.$$anonymous$$EYBOARD)
moveUpOrDown = Input.GetAxis("Player 1 $$anonymous$$eyboard Input");
// Get input from Player 2's input on keyboard
else if (typeOfPlayer == PlayerNumber.PLAYER_TWO && typeOfInput == InputType.$$anonymous$$EYBOARD)
moveUpOrDown = Input.GetAxis("Player 2 $$anonymous$$eyboard Input");
// Get input from the mouse (up or down)
else if (typeOfInput == InputType.$$anonymous$$OUSE)
moveUpOrDown = Input.GetAxis("$$anonymous$$ouse Y");
// Get input from the joystick
else
moveUpOrDown = Input.GetAxis("Vertical Joystick");
speedVector = new Vector3(0.0f, moveUpOrDown * Time.deltaTime * 2.8f, 0.0f);
board.transform.position = new Vector3(board.transform.position.x,
board.transform.position.y, $$anonymous$$athf.Clamp(board.transform.position.z + speedVector.y, $$anonymous$$YPosition, maxYPosition));
}
}
Basically, I am binding $$anonymous$$athf.Clamp to what the new paddle's Z position would be in this 3D vector I'm assigning to the board's position, which is kind of similar to board.transform.Translate. I am doing this because I still want Unity's model to control the acceleration on the board.
I am pondering about program$$anonymous$$g this in a different way; don't game development companies care about how I code and what approach I use in a game engine? Otherwise, I remember posting the same problem onto a Facebook group, and one user said to me I should never use the position of a rigidbody's transform in FixedUpdate since FixedUpdate is primarily for physics.
Let me try this out first, because I want to make a fair bit of progress on my Pong clone before I watch and follow another Unity tutorial. I still feel that there's a lot of work ahead of me as I want to take it on, but I'm taking it slow because I got a university term to deal with at the same time.
Your answer
Follow this Question
Related Questions
Gameobjects transform position minus range 1 Answer
How can I move an object in the direction another object is facing. 1 Answer
How do I make two objects share two of the same transform values? 1 Answer
Bullet shooting not working 0 Answers
how to change the velocity of a rigid body based on a forward 0 Answers