- Home /
Changing who serves in multiplayer pong
,So, I'm making a simple game of pong to help me learn Unity better, and I can't figure out how to switch which paddle gets to serve after each point scored. It's meant to have the ball follow on the paddle, then when the control button is pressed, it shoots the ball and cancels the while loop:
void StartGame()
{
Serve();
}
public void Serve()
{
if (ServeVar == true)
{
this.transform.position = P1Paddle.transform.position + new Vector3(1f, 0f, 0f);
while (ServeVar == true)
{
if (Input.GetKey(KeyCode.RightControl))
{
ServeVar = false;
this.GetComponent<Rigidbody2D>().velocity = new Vector2(8f, -2f);
}
}
}
if (ServeVar == false)
{
this.transform.position = P2Paddle.transform.position + new Vector3(-1f, 0f, 0f);
while (ServeVar == false)
{
if (Input.GetKey(KeyCode.LeftControl))
{
ServeVar = true;
this.GetComponent<Rigidbody2D>().velocity = new Vector2(-8f, 2f);
}
}
}
}
Unity freezes, presumably because of an infinite while loop. I'm pretty sure there's something I'm missing here?
Answer by Nomenokes · Jul 23, 2018 at 10:29 PM
In Unity and most programming languages, code is executed mostly linearly. So the entire Update function of one object is run, then the next, and so on. That means your while loops are infinite because nothing else gets done while they run. The inputs aren't even tested because the entire program is stuck running the while loop.
Instead, you could do something like this:
//the ServeVar class variable is no longer in use, delete it. It is now a local variable within the Serve method
void Update(){
if(Input.GetKey(KeyCode.LeftControl)) Serve(true);
if(Input.GetKey(KeyCode.RightControl)) Serve(false);
}
public void Serve(bool ServeVar){
//... your other code without the while loops, because the serve option has already been chosen
}
The Update function works as a continuous loop until an option is chosen.
Oh, I was typing my solution when this came out. This one is way better.
Answer by SoshJam · Jul 23, 2018 at 10:31 PM
Simple solution: Never use While loops. I can never get them to work.
Here's what you should do: Put this at the top of your code: public bool shouldServe;
Then, in the Start function, replace Serve();
with shouldServe = true;
Just before the Serve()
function, type this:
void Update()
{
if (shouldServe)
{
Serve();
}
}
This tests if the Serve function should be ran, and if it should, then run it.
Next, delete all the while
loops and corresponding brackets. Somewhere in your keypress statement, add shouldServe = false;
so the game knows it doesn't have to serve anymore.
When you detect when the ball goes off-screen (and someone gets a point) remember to add shouldServe = true;
so that it knows to serve again. If it is in a different script, reference it accordingly.
Hope this helps!
Beat you by one $$anonymous$$ute, I know the feeling, sorry
Your answer
Follow this Question
Related Questions
Map looping 1 Answer
Stop animation after playing once 1 Answer
iTween: uneven looping with PutOnPath 1 Answer