About 'Launch The Ball'!


#1

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 you found good about this lecture?
  • 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!


#2

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] Paddle paddle1;
[SerializeField] float xPushMin = 0f;
[SerializeField] float xPushMax = 2f;
[SerializeField] float yPushMin = 10f;
[SerializeField] float yPushMax = 15f;

//state
Vector2 paddleToBallVector;
bool hasStarted = false;

// Use this for initialization
void Start ()
{
    paddleToBallVector = transform.position - paddle1.transform.position;
}

// Update is called once per frame
void Update ()
{
    if (!hasStarted)
    {
        LockBallToPaddle();
        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()
{
    Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
    transform.position = paddlePos + paddleToBallVector;
}

}


#3

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?


#4

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.


#5

Challenge:

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

public class Ball : MonoBehaviour {

    //  config params
    [SerializeField] Paddle paddle1;
    bool hasStarted = false;

    // state
    Vector2 paddleToBallVector;

	// Use this for initialization
	void Start ()
    {
        paddleToBallVector = transform.position - paddle1.transform.position;
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (hasStarted == false)
        {
        LockBallToPaddle();
        LaunchOnMouseClick();
        }
    }

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

    private void LockBallToPaddle()
    {
        Vector2 paddlePos = paddle1.transform.position;
        transform.position = paddlePos + paddleToBallVector;
    }
}

#6

I notice the same thing. If I just leave it alone it constantly builds upwards momentum after every bounce. It’s small, but it’s noticeable. It’s builds up even faster if it’s hitting something at the top as well.