That is independent from your code. That issue is related to friction. This video explains it in detail: