How to Offset collision on player Sprite
Oddly specific question. I have set up a 2D game where all the tiles are 16x16. I have set up tilemap colliders on the grid, and box collider/rigidbody on my player. My player size is 16 x 32, and I have set the box collider to only be located on the bottom 16x16 tile. For some reason, my character still gets stuck below where I want them to go. I have attached screenshots of the issue
Current: The player is standing two tiles below the object. I am unable to press up to move up a tile.
Fixed: The player will be standing one tile below the object (the top half of the player is masking the object)
What is the issue here? My thoughts are that the collision is detecting with the player sprite and not the box collider, but I am unsure how to fix it (if that is actually the issue here). Thanks in advance!
Answer by tylerdtrudeau · Mar 05, 2021 at 09:25 PM
It appears that what is happening is what you already tried to (and should have) fixed. Moving your collider down to the bottom of the sprite would have been my go to answer....but seeing as you already did that.... Is the collider on the item above your character set correctly? If that got moved down as well then that would probably be your issue. Sometimes the colliders can do some funky things where they are offset from the sprite. The Sprite isnt "collidable" so it wouldnt be a sprite issue. It would be a collider issue either attached to that sprite or to other game objects. Just to be sure though, you can make another game object, attach a collider, attach a 16x16 sprite to it then finally attach your player controller script to it. play around with this new "player" and see if everything is working correctly. if it is, then you know it was that original player. You can just delete the 16x16 sprite off the new game object and drag the 16x32 character sprite onto it and test again. (you will need to adjust the y position of the sprite to move it up as its center will be on center of game object. But the noce thing about having the sprite as a child of the player game object is that you can change it all you want and you player functionality wont be affected)
Thank you, you helped me understand what the problem was. For anyone else that might have the same issue in the future:
Instead of attaching a sprite renderer to the player, I created a new sprite as a child of the player game object. Doing that made it work correctly! Thanks again Tyler!
Your answer
Follow this Question
Related Questions
2D Sprites: Animator sync two animations using layers 1 Answer
How to set a tag to a collision? 0 Answers
[Help] Merging multiple sprites to create a long single background 0 Answers
Looking for a proper way to solve jumping collision issue in fighting game. 0 Answers
Rect or Camera transform? 0 Answers