Launch The Ball (Complete Unity Game Developer 2D)

Hi all, i’m about 1 month on unity course but still didn’t understand about this part:

void update ()
{
  if (!hasStarted != true)
  {
   LockBallToPaddle();
   LaunchOnMouseClick();
  }
}

private void LaunchOnMouseClick()
{
 if(Input.GetMouseButtonDown(0)
 {
   hasStarted = true;
   GetComponent<Rigidbody2D>().velocity = new Vector2(transform.position.x , transform.position.y);
   transform.position = paddlePos + paddleToBallVector;
 }
}

on that course thats described that if hasStarted are false it will lock the ball and if hasStarted is true it will run the ball or running LaunchOnMouseClick() method.

How can be like that? still didn’t get it because that update method only include if (!hasStarted) how can LaunchOnMouseClick() can be run on void Update?

Thanks in advance…

Hi Veen,

Welcome to our community! :slight_smile:

Before I start explaining anything: Does your code work? If not, make sure the method names are spelt correctly. In the code you posted, the Update method must be called “Update” with a capital U.

Furthermore, the condition of the if-statement in the Update method must be (hasStarted != true); alternatively, (!hasStarted).

Our Update method gets called each frame. This means that the if-condition gets evaluated each frame, too. As long as the result is “false”, the if-block gets executed, and therefore also the LaunchOnMouseClick code block. And that’s how the ball gets locked to the paddle.


See also:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms