Skip to content

Unity Resource: LangmanController

August 26, 2010
tags:

Unity LogoWhile 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.

Advertisement
13 Comments leave one →
  1. Corey Pullman permalink
    November 20, 2010 12:48 pm

    You are awesome. Good job and thank you.

  2. January 8, 2011 2:07 am

    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.

  3. Chris permalink
    April 13, 2011 1:58 am

    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

  4. Chris permalink
    April 13, 2011 7:04 am

    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

    • Ehren permalink*
      April 13, 2011 7:43 pm

      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);
      }

  5. outofthebox permalink
    May 31, 2011 4:12 pm

    does “As usual, feel free to use this code in your own projects.”
    mean I can use this for commercial

    • June 1, 2011 11:26 am

      Yep, feel free to use it in commercial projects.

      • outofthebox permalink
        June 1, 2011 5:28 pm

        thankyou so much!

  6. Celso Filho permalink
    September 22, 2011 7:19 am

    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.

    • September 22, 2011 12:50 pm

      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.

      • Celso Filho permalink
        September 22, 2011 1:20 pm

        Thanks for the reply. I will try and send the result. Again thank you.

  7. Celso Filho permalink
    September 24, 2011 8:07 am

    I’m new with java script and can not adapt your script to android (touch).

  8. Dietwawa permalink
    December 21, 2011 6:57 am

    Thanks soo much, I am using this in my game! :D

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.