Extreme Tuning with ball velocity changes

I decided to make an option to change the velocity of the ball when it hits a block. I created a slider that can be added to any of the block prefabs and thus can be adjusted individually as well if desired.

You can add the below to the Block script to enable then set to your prefabs. The default “Velocity Increase” of 1 means no change to the velocity.

I created a new block-type called Bumper that uses the below script. It also changes the color of the block.

using UnityEngine;

public class Bumper : MonoBehaviour
{
// Config Paramaters
[SerializeField] AudioClip hitSound;
[Range(0.1f, 2f)] [SerializeField] float velocityIncrease = 1f;

// Cached References
Ball ball;

// State
float mRed = 1f;
float mGreen = 1f;
float mBlue = 1f;

private void Start()
{
    ball = FindObjectOfType<Ball>();
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (tag == "Unbreakable")
    {
        HandleBumperHit();
    }
}

private void HandleBumperHit()
{
    // Create temporary audio object at the camera location (since PlayClipAtPoint plays in 3D)
    AudioSource.PlayClipAtPoint(hitSound, Camera.main.transform.position);

    RandomizeBumperColor();
    ball.GetComponent<Rigidbody2D>().velocity *= velocityIncrease;
}

private void RandomizeBumperColor()
{
    mRed = Random.Range(0f, 1f);
    mGreen = Random.Range(0f, 1f);
    mBlue = Random.Range(0f, 1f);
    GetComponent<SpriteRenderer>().color = new Color(mRed, mGreen, mBlue, 1f);
}

}

1 Like

Thanks for posting this! I was trying to do something similar and was having a super hard time. This is a very cool idea :slight_smile:

Privacy & Terms