Issues with rigidbody, boxcollider, and gravity in a 2d top down game
First of all, forgive me as I am very new to all of this.
I have been following an online tutorial for a top-down zelda-like game. The player's gameobject has a rigidbody2D with a body type of dynamic and a boxcollider2D. Since the game is top-down I need gravity turned off. I have turned y-axis gravity to 0 under edit > project settings > physics, as well as under edit > project settings > physics 2D. I have also set the rigidbody of the character to gravity 0 as well. I have even tried hard-coding a zero gravity into the character's movement model, using Physics2D.gravity = Vector2.zero
, Physics2D.gravity = Vector3.zero
, Physics.gravity = Vector2.zero
, and Physics.gravity = Vector3.zero
. Even after all this, the character rockets towards the bottom of the screen when I hit the play button. The only thing that stops this behavior is turning off the boxcollider 2d, setting the boxcollider2d to "is trigger" or setting the rigidbody to kinematic. While all of these solutions make my movement model work fine, none are really useful since they prevent me from actually using colliders to restrict the player's movement to within the scene. Does anybody have any idea what could be going on? Why is the boxcollider2D forcing my player's gravity back on? Is there a way to stop this, or is there a Kinematic body type workaround? Attached are a couple images, the first shows my game running, the main character already having fell off the scene, the dynamic body type, and the zero gravity. The second image shows the game running, nothing has been changed except that the box collider has been turned off. As I said, similarly, if the box collider is set to "is trigger" or if the rigidbody is set to kinematic, the character stays on screen and my movement model works wonderfully. Unless there is a workaround however, I need a dynamic rigidbody, and some way to turn off the gravity even moreso than I already have.
Any help is greatly appreciated.
I have continued to troubleshoot the problem and haven't yet found the solution. However, I have uncovered an additional clue. After putting a constraint on the y-axis for my character in the rigidbody settings, the character does not fall, as would be expected. But as soon as I press left or right, the character rockets off in the direction pressed. For some reason, as long as the boxcollider is on, the velocity on my character's movement goes crazy, and I can only assume something in my script is putting a slight downward movement on my character as soon as the scene loads. The two effects are combining to make it appear as if he is falling due to gravity, but rather, he is rocketing off towards the bottom of the screen because of the downward push, and the incredible velocity.
Answer by rowanty · Aug 07, 2020 at 08:45 PM
For anybody having a similar problem, I think I've finally figured out what was wrong. After rebuilding the scene from scratch, I noticed that this problem only occurred again once I'd placed more than one boxcollider2D on the RoomParent object that I had created to hold my room template objects. (I was unaware at the time that you can't have more than one boxCollider2D on a given GameObject at a time) I can't explain why it works this way, perhaps someone more experienced than I can, but once I put a CompoundCollider2D on my object, everything started working nicely. Hopefully if anyone else out there runs into this problem, they can find it here! And if you feel you can elaborate or shed more light on my mistake I'd be happy to learn more!
Your answer
Follow this Question
Related Questions
2d collision not functioning correctly 0 Answers
My Unity Rigidbody 2D character fall slowly after a jump,Unity RigidBody Character falls slowly 0 Answers
how to fix velocity for jump and movement or fall in 2d pltform game with gravity 4 Answers
The player falls down a ladder 0 Answers
objects overlap on collision 0 Answers