Fixing Physics?

Hi again!

So, I’ve been trying to tweak the physics of the ball so when it gets caught in either a vertical or horizontal loop the angle tweaks a bit. I have something working for horizontal loops, but it’s not exactly ideal. When trying to apply it to Vertical loops, the ball does all kinds of weird things.

 private void OnCollisionEnter2D(Collision2D collision)

        Vector2 angleTweak = new Vector2
            (Random.Range(-randomFactor -1, randomFactor+1),
             Random.Range(-randomFactor-goDownFactor, randomFactor-goDownFactor));


        if (hasStarted)
            AudioClip clip = ballSounds[UnityEngine.Random.Range(0, ballSounds.Length)];
            myRigidBody2D.velocity += angleTweak;
            coly2 = coly1;
            coly1 = transform.position.y;
            colx2 = colx1;
            colx1 = transform.position.x;
            stuckBall = coly2 - coly1;
            //stuckBall2 = colx2 - colx1;
            if (stuckBall <= loopRange && stuckBall >= -loopRange)
                goDownFactor = goDownFactorOn;
                goDownFactor = 0;

            /*if (stuckBall2 <= loopRange && stuckBall2 >= -loopRange)
                goSideFactor = goSideFactorOn;
                goSideFactor = 0;


And then there’s my parameters:

[SerializeField] float coly1 = 0f;
    [SerializeField] float coly2 = 2f;
    [SerializeField] float colx1 = 0f;
    [SerializeField] float colx2 = 2f;
    [SerializeField] float stuckBall = 0f;
    [SerializeField] float stuckBall2 = 0f;
    [SerializeField] float loopRange = 1f;
    [SerializeField] float goDownFactor = 0f;
    [SerializeField] float goDownFactorOn = 20f;
    [SerializeField] float goSideFactor = 0f;
    [SerializeField] float goSideFactorOn = 20f;

Basically what I have set up is that when the ball collides with an object, it’s Y value is recorded. Upon second collision that Y value is recorded. I subtract one from the other, and if the result fits within the parameters, I add something to the already made Tweak Vector.

This isn’t a perfect system, as occasionally the addition to the tweak is still on when it hits the paddle, and the ball slides off rather than bounce due to the added downward momentum.

If anyone has fixed the loop problem, I would love to know how to do it.

Why have a random tweak at all? If the ball needs adjusting, give it a slight adjustment. Modifying a random value with a - to + rabge means you can still get results close to 0, which makes things boring.

Honestly I was just adding on to the code I had already, kinda unsure what to get rid of and such. But that makes sense. I just don’t know how to execute a code with out it at the moment.

What I did was to check for near 0 velocity in each axis to prevent endless vertical or horizontal bouncing. A couple lines of code and the result works, player won’t notice anything odd. One of many methods I’m sure, but works for me.
Within Ball.cs, in Update()

    if (hasStarted == false)

        if (Mathf.Abs(myRigidbody2D.velocity.x) < 0.25f)
        if (Mathf.Abs(myRigidbody2D.velocity.y) < 0.25f)

I should mention that the TweakVeloctyX/Y routines adjust by a random range between -1.0 to 1.0. If the adjustment isn’t enough to get above the ±0.25f threshold in the current frame, then it’s adjusted again in subsequent frames until it is.

Privacy & Terms