Unity Resource: LangmanController
While developing Langman, much of my time was spent tweaking the PlatformerController script from the 2D Lerpz tutorial. I made some changes to how it works, and along the way I encountered (and fixed) a number of bugs in the original script. I’m sharing my changes in the hope that others will find them useful.
You can download the sample project here. As usual, feel free to use this code in your own projects.
Click here to see the sample project in action.
Primary changes:
- In-air movement is calculated more like running movement. When you release the arrow keys, horizontal movement gradually slows to a stop. The old logic was suitable for a jetpack, but not a traditional platformer.
- EDIT: The jumping logic now allows jumps slightly after the character leaves a ledge.
- EDIT: The script now uses Time.smoothDeltaTime instead of Time.deltaTime.
- I’ve stripped out a bunch of things I didn’t use, such as having different walk vs. run speeds, the particle effect stuff, etc.
Bug fixes:
- Respawning from a moving platform sometimes caused the player to be affected by the platform’s position in the respawn frame. To remedy this, activePlatform is set to null in Spawn.
- When standing on a platform, the character could be pushed through walls and other obstacles. To remedy this, the effect of a platform’s movement on the character is not directly applied to the transform. Instead, it is combined with the other movement input and passed into CharacterController.Move, which handles collision detection.
- EDIT: It turns out that for moving platforms, transform.position must be updated directly. If this isn’t done, the character will eventually fall through the platform. The code now modifies transform.position directly when the character is standing on a kinematic rigidbody that has a non-zero velocity.
- If the character brushed a block diagonally when jumping, he could end up floating slowly upward. This unintentional flying ability was remedied by detecting ceiling collision as part of determining whether the apex of the jump has been reached.
- In certain cases, when jumping while standing on a falling block, the character could end up jumping/landing without ever leaving the block, making it seem like the jump key wasn’t responding. This was fixed by getting rid of inAirVelocity and not making the jump velocity dependent on the velocity of the active platform.
I also modified PlatformerPushBodies. The primary changes I made were to comment out the layer stuff since I wasn’t using it and to only change the velocity of the rigidbody being pushed if the rigidbody’s velocity was less than the character’s. Without the latter change, rollable spheres would “stick” to the character when the character stopped moving.
The project also includes a double-sided vertex-lit shader, which is used for rendering both sides of the character sprite.

You are awesome. Good job and thank you.
Fantastic…simply fantastic. We need more guys like you. Numerous people like me who are hopeless at any 3d modelling softwares can benefit greatly from this…thanks a tonne.
Hey Great work on fixing some of the bugs in the platform controller
it work heaps better for me now
i was wondering tho (im newish to unity) how would i go about accessing the ‘gravity’ var from another script on another object
Thanks
Chris
also i cant get your spawn point to work ?
i draged my object into the inspector and on to spawn point var but it doesn’t spawn there when i run the game
For the spawn point, try calling Spawn().
To access the gravity from another script, you’d first need a reference to your character game object. Then you could do something like:
#pragma strict
var character : GameObject; // Assign this via the inspector
var controller : LangmanController;
function Start(){
// Get a reference to the controller script
controller = character.GetComponent(LangmanController) as LangmanController;
// Print the gravity value
print(controller.movement.gravity);
}
does “As usual, feel free to use this code in your own projects.”
mean I can use this for commercial
Yep, feel free to use it in commercial projects.
thankyou so much!
Excellent work, your changes have greatly improved the script, but I’m having much difficulty to add buttons in touch (Gui texture). I’m not able to replace the variables (var h = Input.GetAxisRaw (“Horizontal”), and (var jumpButton Input.GetButton = (“Jump”). Help me to spread your script for developers of mobile devices.
For the horizontal axis, you need to translate the pressing of the GUI buttons into a numeric value between -1 and 1. A simple way to do this would be to set h to -1 if the left button is pressed, and to 1 if the right button is pressed (otherwise set it to zero). Same thing with jump, except that it will either be 0 or 1.
It would be even easier if you could use the Input menu to bind certain touch buttons to certain input axes (such as “Horizontal” and “Jump”), but I’m not sure if that’s possible.
Thanks for the reply. I will try and send the result. Again thank you.
I’m new with java script and can not adapt your script to android (touch).
Thanks soo much, I am using this in my game!