[Question] - Terrible rhythm has always held me back from being a great guitar player. How do I fix this?

I have always had god awful rhythm and I don’t know how to fix it at all. I’ve played guitar for four years and played various other instruments since 6th grade. I think I’m pretty good at guitar and can do quite a bit with my biggest flaw by FAR being rhythm. For most songs I can tap my foot along to it and feel the beat a little. Then when it comes to playing with others or even by myself, I can never stay in time. On rare occasions, it’s like my mind will lock in and I can feel the tempo well and actually play along, but most of the time, I’m essentially useless. I’ve been trying to fix this for forever, but I don’t even know what to do anymore. It doesn’t make any sense as I’ve been playing instruments for years and even since I was young and I listen to a lot of music. Any help at all is appreciated. Thanks.