Player Floats for No Reason

I’m new to the Unity Engine as I need to use it for my game design classes. I’m posting this here because I feel this is a full-on bug in the Engine and not on my part. I’ve spent maybe 2-3ish hours going through everything.

As you can tell by the title, my character floats for no reason. My player has a RigidBody2D component added and a BoxCollider2D component. The box collider is set for the outline of my player. Since I’m making a 2D platformer, the tile maps have a TilemapCollider 2D component, and a CompositeCollider2D added (which automatically adds the RigidBody2D to it).

I’ve honestly tried everything I can think of which is why I’m sure it’s a bug. I’ve reset and readded all the components for both objects. I’ve played around with the Layers. I’ve cross checked everything in the Animator. I’ve looked over my movement script. I’ve changed the editor’s version. I’ve even increased the gravity scale in the Physics2D option in the Project Settings. There is no reason why this should be happening. I’ve even added the exact same stuff to another project, and it works perfectly. No problems.

Here is the Components view for my Player.

It just makes no sense to me. I’ve tried everything and I’m about to just start from scratch.

Does anyone have any ideas or suggestions? Thanks!

Since you have gravity on above, turn off your player control script and most likely you will no longer float.

If that’s the case, it’s a bug in your player control script, fix it.

If you are floating, press pause as soon as you notice the floating and go look in the scene at your colliders (green gizmos). Are they substantially bigger than (or significally offset from where) you thought they were? Fix that.

Beyond that, it’s simply time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Does it appear to float around 1.05 units from the ground?

It wasn’t this. I also would try to move the player while playing and it’d instantly return to where it was. No matter where it would be, it’d just float. Definitely was a bug in the engine. Like I said, I extensively debugged the movement script and the other components. They weren’t the problem.

well… if you dont answer questions to try help you…

No; it’d just float wherever the player was before hitting play.

Thats not what i asked.

I’m sorry…? You asked if it floats 1.05 units above the ground. I said “no, it floats wherever the player is before hitting play.” In my original post I said that RigidBody2D was not applying gravity. You could raise the player to something like (0, 10, 0) and it’d still be at (0,10,0) after hitting play. You can move on the x axis but not the y.

Second guess is you have your actual physics2d gravity set to zero somewhere. Print it out, find out!

If all else fails, add a ConstantForce2D to the object to simulate your own gravity and be done with it. If that doesn’t work, make a blank repro project and file an actual Unity bug.

As always, create (or have at hand) an empty project. Add a Rigidbody2D and hit play and it should fall. It’s something you’ve done here and it’ll be something which is constantly assigning the position of the Rigidbody2D or the Transform position. In short, start with the basics and then start removing things a piece at a time from your project. You should be using source control to help you to restore it when you’ve done that.

I see an Animator so ensure that’s not stomping on the Transform. Again, disabling things to check.

See if it only applies to this Rigidbody2D. Add a Rigidbody2D on its own and hit play. If it moves then it’s something specific with the other one. If it doesn’t then you may have disabled physics updates which you can find in the Project Settings > Physics 2D > Simulation Mode. Or look at the same project settings and ensure that Gravity isn’t set to (0,0). The default is (0,-9.81). You can reset all thoses settings to default in the top-right corner of it (…).

All the above is essentially trying basic, logical debugging things first. It’s easier than reaching for the forums and having to wait for replies. :slight_smile:

2 Likes

Apologies for the late reply! I did copy over the same scripts and everything to an empty project and it all worked perfectly. It was just in this one project that it wasn’t working, which was why I thought it was an Engine bug. I also double-checked the animation and that I was using 2D components, along with the physics settings. Additionally, when I restarted the engine, it worked.