Sponsored By

Second post for rewind system.

Derek Dittmer, Blogger

February 14, 2013

6 Min Read

Alright, I'm back. I've got a lot of work done on the platformer, but not much actual work done on the rewind system.

First let me talk about how I made the basic platformer. I thought it would be easy; "boring stuff" as I called it in my last post. It always seems easy when thinking about it, but until you actually start making something, you never find out how much work it really is. Because of this, I seem to always bite off more than I can chew.

Anyway, the first thing I needed was movement. I had left and right movement, and was about to start doing jumping, when I decided to go to see if there were any tutorials on Google. I knew I could do it myself, I've done it before, but rather than do what I already knew, I was sure I could learn something if I took a look at another way of doing it. Of course, there were tons of different tutorials on Google, but I found one that looked very well done. It used a lot of things that I didn't know about, so I had to learn about them.

The first thing was, he was using a CharacterController. I've never used one of them before, but they are very useful. Looking into the Inspector, it seemed that all it is is a hit-box and some slope settings, so you can walk up stairs and whatnot. It wasn't until I got into the code that I found how useful these CharacterControllers are. It has its own Move function, but my favorite was the isGrounded check. It checks if you are on the ground, which is much easier than me trying to check if the character is hitting something, and then checking if that thing is the ground. Very useful; glad I took the time to learn about CharacterControllers.

The next thing I learned about was InputManager. The tutorial was using Input.GetAxis() a lot, and I had no idea what that was. It is actually a very flexible way to get input, and there is already a lot of functionality built right into Unity. Input.GetAxis("Horizontal") basically checks what horizontal movement buttons are being pressed: it is negative when you are going left, and positive when going right. That made it easy to find out where the player was going. There were a lot of other things you could play with like acceleration/deceleration when pressing/ not pressing the move buttons. I played around with most of it, just to get a feel for it. I also found that the player can set these input buttons before they start the game, so it is very flexible.

Other than those two things, I could figure out the rest of the code pretty easily, and when I got it working, it worked very nicely.

The next thing I wanted to do was add moving platforms. "More complex movement" to rewind I think I said. So, this moving platform moves with the animation editor in Unity. It moves in a 'n' like pattern. Well, that was easy - except one thing: the player does not move with the platform. I tried just about everything, changing the objects' mass, putting rigidbodies and different colliders on the objects. Nothing seemed to work. When I get to this point, I ask for advice from my old friend Google. There were a lot of different ways to get something to follow a moving platform, but the one that seemed the easiest was just parenting the player to the moving platform when you hit it. I was trying to find a good way to find if you are hitting a moving platform, but ended up just giving moving platforms a tag. That, combined with the CharacterControllers isGrounded, made it pretty simple from there. It now works like a charm.

bat Next up is the enemy. Overall, pretty simple, though I did have trouble with its collision. I tried when you hit a wall, just move the other direction, but the floor is made up of small blocks, and the enemy would just keep hitting different parts of the floor and get stuck. Then I remembered seeing that there were flags for which side you got hit in the CharacterController (CollisionFlags.Sides). I thought that would make it easy to just have an enemy walk back and forth until it hit something. And I was right, it did make it easy, but trying to test if it hit the player was not. I am guessing that there is only supposed to be one CharacterController, because they don't collide very well I found. I ended up just parenting a box collider to the enemy, and everything worked perfect.

Like the first enemies in Braid, you kill them by jumping on them, and they also give you a little jump boost. They can also kill you if you are hit by its sides. This caused all kinds of problems for me. I tried using the CollisionFlags.Sides, but that was not working very well. I actually ended up just checking if the players y position + its collider bounds was greater than the enemy, then kill the enemy, otherwise player is dead. That worked out very well. So, now I have an enemy!

Now, I wanted make a key and have that key open a chest to win. There wasn't any huge issues with this. I made the key parent itself to the character when you hit it, then if the key hits the chest, open it. The only thing I had to change to the player was make it scale its x-axis by -1 so that the key would look like it is staying where you are going and not just floating there. It is similar to how Braid does it.

The next thing I wanted to do was clamp the frames per second. I don't know what number to clamp it to yet, but I wanted to make sure I could. Application.targetFrameRate made it very easy to do that. Then I went to the Unity wiki and found a show frames per second script that works very well. Now I can see how fast it is going, and how fast I will be saving the players position for the rewind.

 The last thing I added was some simple 2d animation for the player and the enemy. I couldn't find any good ways to do it online, so I just did it myself. I'm really happy how it turned out. It is just a base class that anything can inherit. I got it all set up for the player, and then all I had to do was set the frames for the enemy, inherit from the animation class, and it just worked. Nice!

I put some textures on everything, because the white, untextured blocks looked weird with the nicely animating player and enemy. Now it is time to start making these things rewind!

(To see/play/download the project of final product, go to my website: http://derek.dittmer.com/please-be-kind.html

Read more about:

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like