Vector2 tweakVelocityOfBoth = new Vector2(Random.Range(0.1f, randomFactor), Random.Range(0.1f, randomFactor));
Vector2 tweakVelocityOfX = new Vector2(Random.Range(0.1f, randomFactor), 0);
Vector2 tweakVelocityOfY= new Vector2(0, Random.Range(0.1f, randomFactor));
myRigidBody2d.velocity = myRigidBody2d.velocity.normalized * ballSpeed;
if (hasStarted && myRigidBody2d.velocity.x >= 0 && myRigidBody2d.velocity.y >= 0)
{
myRigidBody2d.velocity += tweakVelocityOfBoth;
//Debug.Log(myRigidBody2d.velocity);
}
else if (hasStarted && myRigidBody2d.velocity.x <= 0 && myRigidBody2d.velocity.y <= 0)
{
myRigidBody2d.velocity -= tweakVelocityOfBoth;
}else if(hasStarted && myRigidBody2d.velocity.x >= 0 && myRigidBody2d.velocity.y <= 0)
{
myRigidBody2d.velocity += tweakVelocityOfX;
myRigidBody2d.velocity -= tweakVelocityOfY;
}
else if (hasStarted && myRigidBody2d.velocity.x <= 0 && myRigidBody2d.velocity.y >= 0)
{
myRigidBody2d.velocity += tweakVelocityOfY;
myRigidBody2d.velocity -= tweakVelocityOfX;
}
It applies tweakVelocity in all direction
Ball never gets stuck!