Collision hotfix


Other than improving the bouncing of the ball by not making it totally random, I also noticed while play testing that sometimes the ball would decide its collision too early. This was due to me implementing my own "physics", which totally is not just an AABB check every valid frame; because I wanted more control over how the ball was bouncing and also because it's an overkill to use the PhysiX engine built into Unity.

This came with it's own little caveats of course. Number one being the simplicity. I could no longer just tick a checkbox and voila I have interpolation. 

I had to implement it and I have to say it was much easier and more intuitive than I thought.
Here's my implementation:

float disToMove = _velocity.magnitude * Time.deltaTime;
int iterCount = (int)(disToMove / 0.09f); //0.09f is not a magic variable but the approx. distance to the shortest diagonal vertex of a 0.25x0.25 square
RaycastHit2D hit = new RaycastHit2D();
if (iterCount > 0)
{
    for (int i = 1; i <= iterCount; i++)
    {
        float disToCheck = i * 0.09f;
        hit = Physics2D.BoxCast(transform.position, Vector2.one * 0.25f, 0.0f, _velocity.normalized, disToCheck);
        if (hit) break;              
    }
}        
else
    hit = Physics2D.BoxCast(transform.position, Vector2.one * 0.25f, 0.0f, _velocity.normalized, disToMove);

Files

PongBuild.zip Play in browser
May 22, 2023

Leave a comment

Log in with itch.io to leave a comment.