In this video (objectives)…

1. Create a method to give the ball an upwards velocity.
2. Create a bool which allows us to determine if the game has started yet and therefore stop locking the ball to the paddle.

After watching (learning outcomes)… Launch the ball with upward velocity from the paddle.

(Unique Video Reference: 11_BR_CUD)

We would love to know…

• What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

• Be in the correct forum (for the course).
• Be in the right sub-forum (for the section)
• Have the correct lecture tag.

Enjoy your stay in our thriving community!

I wanted the ball to leave the paddle in somewhat random directions. Not too crazy. So here’s what I did…Seems to work well. Even though I intialized the min’s and max’s in the code, the sweet spots seem to be:

xPushMin = -5
xPushMax = 5
yPushMin = 15
yPushMax = 20

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour
{

``````//config parameters. serialized a min and a max for both x and y
[SerializeField] float xPushMin = 0f;
[SerializeField] float xPushMax = 2f;
[SerializeField] float yPushMin = 10f;
[SerializeField] float yPushMax = 15f;

//state
bool hasStarted = false;

// Use this for initialization
void Start ()
{
}

// Update is called once per frame
void Update ()
{
if (!hasStarted)
{
LaunchOnMouseClick();
}

}
private void LaunchOnMouseClick()
{
if (Input.GetMouseButtonDown(0))
{
``````

// used Random.Range to randomize the direction of the ball as it leaves the paddle
hasStarted = true;
GetComponent().velocity = new Vector2(UnityEngine.Random.Range(xPushMin, xPushMax), UnityEngine.Random.Range(yPushMin, yPushMax));
}
}

``````private void LockBallToPaddle()
{
}
``````

}

Hi Rick,

I noticed that the ball keeps accumulating speed as it bounces off the paddle and keeps getting higher and higher. How would you go about making it bounce back to the same height?

I wouldn’t worry too much about it yet, we’ll have tuning to do once we have blocks in the scene. But for me, when I have bounce set to 1, the ball seems to bounce up to the same height each time off the paddle.

Challenge:

``````using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour {

//  config params
bool hasStarted = false;

// state

// Use this for initialization
void Start ()
{
}

// Update is called once per frame
void Update ()
{
if (hasStarted == false)
{
LaunchOnMouseClick();
}
}

private void LaunchOnMouseClick()
{
if (Input.GetMouseButtonDown(0))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 15f);
hasStarted = true;
}
}