Platformer Controllers: How do you do it?

I am in the process of coding a 2d sidescroller platforming game, and I want to get a general overview for how some other people do things.

I know Unity provides the fantastic character controller, however, for most of us it is just better to code a controller from scratch, I think. This offers us much more control over how we want it to behave exactly.

With that in mind, how do you guys go about coding your own character controller for a platforming game? What function do you use to move the character? Do you use Translate? Do you mess with the velocity? How do you prevent “spazzing out”, and getting hung-up on walls? How do you handle collisions? Jumping?

Hopefully we can communicate how we go about coding a character controller from scratch for complex platformers here. Feel free to post code.

Thanks!

I personally use a rigidbody with disabled rotation, and add forces to reach top speeds or jump. It can have some complex math behind it, but with some proper formulas it’s very satisfying, and can even out-perform the built-in controller.

Thank you! This is by far the most stable way to do things in most cases, it seems.

I did this very thing last week and it mostly worked okay. My big difference was that instead of using addforce I set velocity directly. My only real problem with this is that it made jumping into walls “sticky”. I mostly solved that by giving the player an ice physics material which is a 90% solution… still not entirely happy but it works well enough.

What i did run into that was a mystery was how you properly make elevators. See this thread for more details

http://forum.unity3d.com/threads/116980-Elevators-unity-animations-vs-Vector3.Lerp-Slerp

I created a nice platformer controller by using raycasting and then calculating if I would hit something the next frame. If that was the case I would not move the character. No physics was used and I got great performance. Read more in my thread:

http://forum.unity3d.com/threads/102657-Creating-a-platform-game-raycasting-vs-colliders-and-performance

I’d personally be willing to sacrifice a little performance for better physics. I’m fairly certain the rigidbody method is actually about the same performance as a built-in character controller, but can have much more emergent interaction. I’m still doing a great deal of testing on the subject.

Go here, period

http://www.walkerboystudio.com/html/unity_training___free__.html#unity3game3

That tutorial uses the character controller. Which is great when you do not, like I did, need like 50+ characters on screen in an iPhone game. If that is not the case, use the character controller.

For maximum performance in respect to Collision detection in a 2D game, your best option is to create your own Collision system provided you’re familiar enough with UnityScript or C#.

In this case the Rect Struct and its Contains method is your friend here.

By using 2D Textures and focusing on screen coordinates instead of world coordinates you can produce pixel precision collisions. Each “physics” object in your scene can register on a static array and you can have a Collision Controller checking each object’s position versus every Collider on the scene. This is sure to give you a huge performance boost.

I create custom Colliders this way, but for a 3D environment in my online tutorials.

Episodes 14 and 17.

While it’s not exactly what you want (since they detect collisions in 3D instead of 2D), it will point you to the right direction.

The character controller does not have any performance issues on the iPhone. If you are having performance issues on iOS, look at your code. Further more why would you use a character controller for each enemy when they only need to be rigid bodies? Sounds like you’re over complicating things perhaps. Personally I use Kinematic Rigidbodies and iTWeen to move the enemies, this even works fast enough to use AI pathfinding, which means I avoid the bugs created by RayCasting in corners.

I tried this but it didn’t give me the control I needed to give the game the correct feeling. One problem I got was that once I detected a collision my enemy would already be inside (partially) the wall, which did not look good. By using raycasting I can detect a collision before it is seen on the screen and avoid moving the enemy there. I can also line my enemies perfectly up against walls instead of moving them through them and then reacting.

And about performance, have you tried using as many as 50 character controllers on the screen at the same time? I have seen (and many others have told me) that after about 10 the game starts getting slower. Can you confirm that as many as 50 work without slowing down the game?

Also about writing your own collision system. I can confirm that it works great, BUT you might run into problems when you start getting alot of objects in the scene. If you want to manually check collisions against every object the game can get really slow when having many objects. This can be solved (I guess) by creating a grid system so that you only check against certain objects, but I took the route of ray casting instead.