Block Braker with Raycasts

Hey guys,

Im making a game similar to block breaker but as my walls are modular, i cant use Unity physics as it keeps hitting the collider corners on each wall piece. The only real way around this is to gut the physics and use raycasts which normalize the contact point so the walls are more uniformed as one surface and not hitting the individual physics colliders.

Has any one got any thoughts on how to code the ball using a raycast + vector3.reflect? Ive had a few attempts with no luck so far.

Assuming that you already know the direction the ball is going in, here:

Vector3 ballDirection;
RaycastHit hit;
Vector3 newBallDirection;

if(Physics.Raycast(ball.transform.position, ballDirection, our hit, Mathf.Infinity)){

newBallDirection = Vector3.Reflect(ballDirection, hit.normal);
}

The above, works off the ball’s original direction direction (before hitting the wall), it gets the raycast hit normal and returns the reflected direction. Hope that helps

@highpockets thanks for you help with this so far. Ive made some progress but the “Ball” is bouncing 180 and not taking into account any angles at all. Any further help would be much appreciated!

    public float bulletVel = 17f;
    public Vector3 bulletDir;
    public Vector3 newBulletDir;
   

    private void Awake()
    {
        //Starting directional movement
        bulletDir = Vector3.right;
    }

    private void Update()
    {
        
        RaycastHit hit;

        if (Physics.Raycast(this.transform.position, bulletDir, out hit, Mathf.Infinity))
        {
            newBulletDir = Vector3.Reflect(bulletDir, hit.normal);
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        bulletDir = newBulletDir;
    }
    private void FixedUpdate()
    {
        transform.Translate(bulletDir * Time.deltaTime * bulletVel);
    }