About 'Block Breaker Instructor Hangout #2'!

In this video (objectives)...

  1. Discussion about the endless loop issue.
  2. Public versus [SerializeField].
  3. What are messages and how are they called.

After watching (learning outcomes)…

Ben and Rick talk about a few issues that came up in this section of the course.

(Unique Video Reference: 37b_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!

These are some of my favorite parts of the course. Keep up the great work!

1 Like

I love watching these. Insightful question about the reflection calls. It’s good to see that in the course.

Great tips by Ben about Laser Defender! :slight_smile:

1 Like

I love these instructor hangouts and how each have ideas on how to solve (or handle) different situations. I loved this section so much and really enjoyed going and adding different features into the game.
Great content so far!! Cannot wait to see what lays ahead!!

Really like Ben’s insight on different ways to avoid boring ball loops!

3 Likes

Thanks, Rick. Really enjoying this course so far.

Regarding the endless loop issue, I’ve been looking at the original Arkanoid game for possible solutions.

One thing I noticed is that the ball never ever travels perfectly horizontally or vertically. I replicated this behaviour by adding an OnCollisionExit2D callback to Ball.cs to make sure that the ball never bounces too close to these directions (within the range of a “fudge factor”).

Here’s the code I wrote. It seems to work quite well, and ensures that the ball always travels at the initial push speed to avoid unexpected slow-downs, which sometimes seemed to happen if the ball got stuck between the paddle and the left or right wall collider.

    [Range(7.0f,15.0f)] [SerializeField] float pushSpeed = 12.0f;
    [Range(0.2f, 0.9f)] [SerializeField] float bounceFudge = 0.4f;

    private void OnCollisionExit2D(Collision2D collision)
    {
        // Prevent ball from following paths that are too close to horizontal
        // or vertical (to prevent boring loops)

        // Get the ball's velocity (after a bounce)
        Vector2 ballVector = myRigidBody2D.velocity;

        // Calculate the angle (in radians)
        float ballAngle = Mathf.Atan2(ballVector.y, ballVector.x);

        if (ballAngle >= 0.0f && ballAngle < bounceFudge) {
            ballAngle = bounceFudge;
        }
        else if (ballAngle <= Mathf.PI * 0.5f && ballAngle > Mathf.PI * 0.5f - bounceFudge) {
            ballAngle = Mathf.PI * 0.5f - bounceFudge;
        }
        else if (ballAngle > Mathf.PI * 0.5f && ballAngle < Mathf.PI * 0.5f + bounceFudge) {
            ballAngle = Mathf.PI * 0.5f + bounceFudge;
        }
        else if (ballAngle <= Mathf.PI && ballAngle > Mathf.PI - bounceFudge) {
            ballAngle = Mathf.PI - bounceFudge;
        }
        else if (ballAngle < bounceFudge - Mathf.PI) {
            ballAngle = bounceFudge - Mathf.PI;
        }
        else if (ballAngle <= -Mathf.PI * 0.5f && ballAngle > -bounceFudge - Mathf.PI * 0.5f) {
            ballAngle = -bounceFudge - Mathf.PI * 0.5f;
        }
        else if (ballAngle > -Mathf.PI * 0.5f && ballAngle < bounceFudge - Mathf.PI * 0.5f) {
            ballAngle = bounceFudge - Mathf.PI * 0.5f;
        }
        else if (ballAngle < 0.0f && ballAngle > -bounceFudge) {
            ballAngle = -bounceFudge;
        }
        else {
            // If we didn't need to change the ball's path, just return...
            return;
        }

        // ... But if we did need to change the path, update it now.
        // NOTE: Also constrain the ball's velocity to its initial value
        // to avoid unexpected changes of speed
        
        ballVector.x = Mathf.Cos(ballAngle) * pushSpeed;
        ballVector.y = Mathf.Sin(ballAngle) * pushSpeed;
        myRigidBody2D.velocity = ballVector;
    }
3 Likes

Privacy & Terms