- Home /
How can I get my BoxCollider2D to stay centered on my sprite?
The problem: After I start my game, the player's BoxCollider2D's y offset changes.
.
Details: It started after I changed the pivot point of the player's sprite from center to bottom center. I did this in order to make the player's death animation, which is a simple 90 degree rotation. Unfortunately, if the issue did occur before the animation was made, I didn't catch it in a quick play test.
Now during the game, the collider seems to shift downward about half of its length. Weirdly, that lowered position is the shape of what collides in the game and what the debug logs show, but the collider always appears in the correct position in the inspector and scene view.
.
Debug log findings: I don't have anything in my code that directly changes the collider's offset, yet debug logs are showing different values for the collider's y offset.
It's in the same correct position which I see in my inspector during the game manager's awake function, then is moved half of its length downward in my mover script's start function. That script does get the BoxCollider2D component, but the debug log shows that the offset is incorrect before the script has even gotten it. That's the only script that touches it, but it doesn't use the offset. The player's start function then prints the correct value, and then my NPC's script grabs the value to print it, and shows the lower/incorrect offset.
.
Tried so far: I tried changing the BoxCollider2D's offset in the inspector to counteract the problem, but then during the game, it snaps into the same y value regardless of my changes.
I changed only the player's transform's rotation in the animation. The box collider does not appear to be errantly rotated during run time, just lowered. I tried running the game with the animator disabled, and the same issue occurred. I tried changing the sprite's pivot point back to the center and adjusting the box collider. It snaps into the same lower position. I didn't include code because I didn't change the scripts around the time the issue started, and I'm not sure which could be the culprit since none mess with the offset. Let me know if you'd like to see code though, I can post it.
.
I hope someone has a fix for this problem. Thank you so much!
Hello @sleepypixie , can you post any screenshots please?
Answer by ZeyFight · Jul 03, 2021 at 02:43 PM
Hmm ... I think we have to put back the pivot correctly and create a death animation without touching the player ... That is to say without modifying the pivot or other directly from the inspector. And lifted your player if you have gravity so that it falls back into place
Answer by sleepypixie · Jul 03, 2021 at 04:23 PM
While taking screenshots for the above questions, I realized that I made a mistake in playtesting. Changing the sprite's pivot point back to the center does solve the problem. The player sprite is selected at the beginning of the game, and I somehow forgot to move the point back to center on the sprite I play tested with the first time. That could explain the collider offset changing during runtime, too. It will be easy enough for me to continue on with making my game with the original centered pivot point.
I am still curious to know why moving the sprite's pivot point moved the box collider if anyone happens to know.
Your answer
Follow this Question
Related Questions
Sprite tiling and offset? 3 Answers
Collision Detection + Transform Offset Question 0 Answers
Sprite animation is not updating (possible bug) 1 Answer
Unity 2d circle collider not stopping after collision with box collider 2d 1 Answer
My Box collider 2D does not follow the sprite when its jumping. 2 Answers