Unity 2D Collision Woes
I’ve decided to cut back on blog posts for a while. Last year, I was trying to make regular updates on Mondays and Wednesdays about the gaming industry, however I feel that the time that I’ve spent researching things to share and talk about (let alone the time actually writing) could be more fruitfully spent trying to do actual development. We will continue to make regular updates on Fridays about what we’re up to.
As mentioned last week, I’m currently in the process of learning Unity. Learning new software (and especially when it’s for a task that you haven’t done before) can be daunting, however I feel that I’m picking it up reasonably well. The hardest part is knowing what questions to ask, and who to ask when starting off with something new. I’ve been trying to follow some tutorials, but am now starting to get to the point where I’ll have to start branching out into the forums to find specific things that I want to learn.
Thus far these are the tutorials I’ve followed:
- Unity’s Roll-A-Ball Tutorial
- Unity’s Space Shooter Tutorial
- Unity Live Training 16 Dec 2013 – 2D Character Controllers
- Prime 31 Studio’s CharacterController2D
- Unity 4.3 2D Tutorial: Getting Started (Zombie Conga)
I’ve used the knowledge gained from the above tutorials to start putting together a test of my own.
The big problem which I mention in the video is that the ground detection and character collider seem to be having problems when at the edge of an object. Sort of having something like a Mega Man sprite that has the feet at the edges of the image, I’m not sure of the best way to deal with the sprite.
- If the ground detection is larger than the collider, then when the character is against the wall and jumps, the ground detection will spot the wall and say “hey, I”m still on the ground” and then it will keep the idle animation active until it jumps above the wall.
- If the ground detection is smaller than the collider, then when the character is standing on the corner of the ledge it will transition into a jump animation, but will still stay collided and on top of the ledge.
- If the ground detection is the same size as the collider, then we seem to get the exact same problem as number 2.
- The Unity tutorial seems to favour having a rounded collider so that when a character is at the edge it will slide off. I however don’t want an effect like this as it looks aesthetically unpleasing with pixel art.
I’ll need to investigate further how I can have a new animation when a character is standing at the edge of a platform, and how I can change the collision bounding boxes depending on what animation we’re looking at.
Some of the successes that I’ve had so far however, have included:
- Delayed Transition between animations: I was following along with the third tutorial for the most part. Just a simple run, idle, jump cycle animations. The problem I had was that when using blend tree transitions the transitions weren’t immediate when switching to either the double jump or the idle poses. The sprite would land, the parameter got set to Ground = True, and then there was a fraction of a second lag where the character remained at ground level in the jump frame of animation before finally transitioning to idle. This wasn’t happening if I instead just used an empty state which had a pose in it. In this case, a transition would be immediate. Turns out there was a very simple fix to this, and that was to make sure that the blue arrow handles that were appearing for the transition were set to be immediately beside each other. They default in a way that is designed to smooth blending in 3D, but just cause delay in 2D.
- Next was an issue with the fact that my game window was set to Free Perspective. This meant that every time I would resize my window in unity it would change my camera resolution and aspect ratio. Not cool! But that was something else which is super easily solved. Make sure that your player settings are set to whatever the resolution is that you need (Edit->Project Settings->Player) and then in your game window make sure that at the top left you set the resolution from Free Perspective to your Standalone settings (or at least a similar ratio if it exists). This will ensure that your window gets sized appropriately for periodic testing.
- Also camera related is the fact that Unity doesn’t make it straight forward to determine your camera’s resolution. I’m going to be working with pixel art, which means I can’t have my sprites being interpolated! Pixels have to be mapped correctly otherwise you start getting gross jitter and pixel stretching. To get around this you have to be aware of the pixel -> unit ratio that you’ve got set on your sprites, as well as the orthographic width of the camera. By default sprites come in at 100 pixels per unit. This means that a character that is 64 pixels tall will be 64/100 = 0.64 units tall. Orthographic cameras need to have a width which is half of your desired unit height. So if you have a camera that should render 480 pixels tall, you need to set your orthographic width to (480/100)/2 = 2.4. You’ll also need to create a Sprite Shader which has the “Pixel Snap” option turned on. Apply this Sprite Shader to the Material that is applied to your Sprite Renderer (attached to your objects and characters).
- Finally, a problem that I was having was that the collision system in Unity is not ideal. If you push the character against the wall, keep pushing forward and jump… your character can get stuck in the wall. You’ll be able to continue jumping to climb the wall. There seems to be a couple of ways around this. I need to explore the set-up in Prime 31’s scripts a little further as they appear to be doing a ray casting that doesn’t cause any problems with this… but otherwise, if you follow the Unity example, you’ll need to add a Physics2D material to your walls so that they have a friction of 0. This prevents characters from sticking and lets them slide down.
Next on my tutorial agenda is:
- Unity 4.3 – 2D Game Development Walkthrough (An example file from unity which has a lot to explore at your own pace)
- 3D Video Game Development with Unity 3D (A Free 50 hour course in Unity 3D development)
I suspect the course is going to be worthwhile for me to do. While I think I’m getting a grasp on moving characters around, I still have no concept of how to do things like maintain an inventory, or transition to different scenes. I’m also going to have to get a grasp on a more jrpg/Link To The Past style camera system as I don’t actually think that this platformer style layout is what I want to be focusing on for my secret project.