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)];
myAudioSource.PlayOneShot(clip);
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;
}
else
{
goDownFactor = 0;
}
/*if (stuckBall2 <= loopRange && stuckBall2 >= -loopRange)
{
goSideFactor = goSideFactorOn;
}
else
{
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.