I am trying to prevent my ball from getting stuck on the X axis. The goal of my method is to adjust the collision of the ball when it is moving at a speed of slower than 0.5F per second on the X axis so that the ball will not enter a forever loop at 0F. What I have come up with so far is:
[SerializeField] float SpeedX;
private void FixMotion()
{
Rigidbody2D glitchFix = GetComponent<Rigidbody2D>();
if (glitchFix.velocity.x <= 0.5f)
{
SpeedX = glitchFix.velocity.x + 0.3f;
}
}
I am trying to call this method upon my collision event of the ball, but nothing is happening currently. I am calling the method here:
private void OnCollisionEnter2D(Collision2D collision)
{
if (hasStarted)
{
AudioClip clip = ballSounds[UnityEngine.Random.Range(0, ballSounds.Length)];
myAudioSource.PlayOneShot(clip);
}
FixMotion();
}
Could someone explain what I’m doing wrong please? I’m struggling a bit with if else statements and decided to take a small break from the course to work on them so I’m thinking the error may lie somewhere in there. Thanks!