Search Unity

Unity Tutorial: Creating a Scroller/Platformer Game

Discussion in 'Community Learning & Teaching' started by Random-username, Jun 19, 2013.

  1. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Hi, my name is Sebastian. Welcome to this series on creating a 2D platformer game in unity.
    I hope you enjoy.

    Full playlist here: http://www.youtube.com/playlist?list=PLFt_AvWsXl0e-g21S-MiPArfEhlv9pUgC
    Here is the current list of videos (the list continues two posts down). Currently up to part 6.

    Part one: Setting up a controller with collisions.


    Part two: Further improving the controller


    Part three: Adding a player model and setting up walking/running animations with Mecanim blend trees.


    Part four: More work with Mecanim; jumping and sliding.


    Part five: Implementing a simple health system with ragdoll deaths.


    Part six: Wall jumping
    https://www.youtube.com/watch?v=7Y3PX9y43y4

    Part seven: Moving platforms and respawning
    https://www.youtube.com/watch?v=iUSzqX3dqrg

    Part eight: Checkpoints and loading levels
    https://www.youtube.com/watch?v=H7Ei77K9jRQ&feature=c4-overview&list=UUmtyQOKKmrMVaKuRXz02jbQ



    Cheers :)
     
    Last edited: Dec 9, 2013
    Sazem likes this.
  2. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    Love it!
     
  3. Super!

    Super!

    Joined:
    Jun 17, 2013
    Posts:
    4
    In the very early stages of developing a Platformer myself so this is fantastic!

    Keep up the good work, can't wait for the other parts!
     
  4. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Part six: Wall jumping


    Part seven: Moving platforms and respawning
     
    Last edited: Dec 4, 2013
  5. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Just uploaded part 3.
    In this part we add in a character and set up some of its animations, as well as make improvements to the player's collision detection.
     
  6. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Just released part 4, in which we use Unity's animation system Mecanim to set up running and sliding animations.
     
  7. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    You need to visit the NYC area so I can buy you a few nice cold beers! Thanks for these. terrific work.
     
  8. joaopaulogalvao

    joaopaulogalvao

    Joined:
    Jun 26, 2013
    Posts:
    4
    Hey man,

    Many thanks, great tutorial. Have you got any plans for a 3D tutorial? Congrats, once again!
     
  9. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Thanks man, no problem. I don't have any plans at the moment, but I'm open to suggestions!
     
  10. TannerJS

    TannerJS

    Joined:
    Jul 31, 2013
    Posts:
    1
    Great tutorial. I've never used Unity before and found it to be easily understandable and was able to work through all 4. I look forward to seeing what you bring to the table with part 5.
     
  11. leebs0117

    leebs0117

    Joined:
    Jul 1, 2013
    Posts:
    7
    Great tutorial !! Thanks !
    waiting for more tutorial !:p
     
  12. Neeko

    Neeko

    Joined:
    Mar 9, 2012
    Posts:
    24
    This is exactly what I've been looking for. I'm coming from ImpactJS and I've been struggling to understand how to do platformers "the Unity way". Thanks for putting these together!
     
  13. Apiweb

    Apiweb

    Joined:
    Nov 26, 2011
    Posts:
    57
    Very good tutorial. Congratulations.
    I would like to make a few suggestions:
    • A tutorial by implementing double jump;
    • A video that explains how to make the character grab the edge of a platform
    • A tutorial that teaches how to change the animation when the character is near the edge of a platform (animation that will fall, for example);
    • And how, for the character jump on a wall, and the wall he can use it as a boost to jump higher.

    Well, they are small somentes suggestions of features that are widely used in platform games. And I think most would enrich this series.
     
  14. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Thanks for the suggestions! I'll definitely cover all of those at some point in the series.
     
  15. gplord

    gplord

    Joined:
    Aug 8, 2013
    Posts:
    1
    Just wanted to add one more thanks for making these. Easily the best 2D series I've seen so far, and this was incredibly helpful with a project I've been working on. Really looking forward to the rest of the series!
     
  16. Tastygraph

    Tastygraph

    Joined:
    Nov 12, 2009
    Posts:
    25
    great job, keep up the good work.

    Your tutorials are easy to follow (although a little bit fast in terms of typing while talking, but nothing a bit of pause/play cant handle on my end :) )

    looking forward to part 5
     
  17. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Part 5 is now complete and added to the list.

    Thanks phase and tasygraphgames!
    Sorry about the speed, I just don't want the videos to drag on. Glad you're still managing to follow along ;)
     
  18. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
  19. rmViper

    rmViper

    Joined:
    Aug 29, 2013
    Posts:
    2
    Thanks for sharing these, they are really helpful.
    I would love to see one about dealing with slopes someday. :)
     
  20. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Slopes are tricky!
    I'll see what I can do ;)
     
  21. Lioncirth

    Lioncirth

    Joined:
    Apr 7, 2009
    Posts:
    111
    Excellent!

    I have been working on my platformer in Playmaker and have decided to try and learn c# - this seems like it will be a perfect series for me :)

    Thanks!!
     
  22. Zastrow89

    Zastrow89

    Joined:
    May 1, 2010
    Posts:
    51
    love it man please dont stop making tutorials! you are GREAT at it! thanks alot for this!
     
  23. peon

    peon

    Joined:
    Aug 6, 2012
    Posts:
    13
    Thanks a lot for these tutorials! Very good stuff. You've really helped me out here.

    If you could make one more tutorial about how to handle slopes I'd be very grateful!
     
  24. lo-94

    lo-94

    Joined:
    Nov 1, 2013
    Posts:
    282
    Really great video series here
    Thanks thanks thanks
     
  25. lo-94

    lo-94

    Joined:
    Nov 1, 2013
    Posts:
    282
    So idk how much this is monitored, but I can't seem to get past the movementStopped bool section. I've gone through my code and compared it to yours with a fine tooth comb and can't find any differences. What would cause the player to stick to the box? I have the if statement before the input section just like it said. Any suggestions where to look? Cause it looks like it should function fine
     
  26. pugdug808

    pugdug808

    Joined:
    Jul 24, 2012
    Posts:
    3
    HELP! what is happening! i have looked through this a million times and cant find something wrong PLEASE HELP!

    CODE:

    using UnityEngine;
    using System.Collections;
    [RequireComponent(typeof(BoxCollider))]
    public class PlayerPhysics : MonoBehaviour {

    public LayerMask collisionMask;

    private BoxCollider collider;
    private Vector3 s;
    private Vector3 c;

    private float skin = .005f;

    [HideInInspector]
    public bool grounded;

    Ray ray;
    RaycastHit hit;

    void Start() {
    collider = GetComponent<BoxCollider>();
    s = collider.size;
    c = collider.center;
    }

    public void Move(Vector2 moveAmount) {

    float deltaY = moveAmount.y;
    float deltaX = moveAmount.x;
    Vector2 p = transform.position;

    for (int i =0; i<=3; i ++) {
    float dir = Mathf.Sign(deltaY);
    float x = (p.x + c.x - s.x/2) + s.x/2 * i;
    float y = p.y + c.y + s.y/2 * dir;

    ray = new Ray(new Vector2(x,y), new Vector2(0,dir));

    if(Physics.Raycast(ray, out hit,Mathf.Abs(deltaY),collisionMask)) {
    float dst = Vector3.Distance (ray.origin, hit.point);

    if (dst > skin) {
    deltaY = -dst + skin;
    }
    else{
    deltaY = 0;
    }
    grounded = true;
    break;
    }

    }
    Vector2 finalTransform = new Vector2(deltaX,deltaY);

    transform.Translate(finalTransform);
    }

    }
     
  27. tobicreaper

    tobicreaper

    Joined:
    Apr 30, 2013
    Posts:
    69
    sir, you are AWESOME. I salute you :D
     
  28. PurpleFlow

    PurpleFlow

    Joined:
    Sep 16, 2013
    Posts:
    7
    Can't thank you enough for these ;__; I'm making my own character model and I'll rig it using your other video tutorial. Do you plan on continuing this series? I'm curious about more gameplay matters like health bars, leveling up etc, but I'm not sure they fit a runner game :p Anyway, whatever you do will be useful!
     
  29. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Hi guys, thanks for the feedback/suggestions.
    Part seven is now out!
     
  30. Zv_oDD

    Zv_oDD

    Joined:
    Dec 5, 2013
    Posts:
    2
    For those of you interested further reading on the theory/ problem solving of 2D platform controllers:
    The hobbyist coder #1: 2D platformer controller
    This is using a similar ray casting solution but gets more in depth dealing with the caveats of slopes.
    I found it very helpful.
     
    danm_3d likes this.
  31. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Thanks for sharing, Zv!
     
  32. MinhDao

    MinhDao

    Joined:
    Oct 28, 2013
    Posts:
    155
    Perfect tutorial, thank you very much! Waiting for your next tutorial.
     
  33. MinhDao

    MinhDao

    Joined:
    Oct 28, 2013
    Posts:
    155
    I have finished 7 parts and waiting for next tutorial! Can you make some tutorial about enemy, AI and attack system? Thanks you very much!
     
  34. Timze

    Timze

    Joined:
    Dec 3, 2013
    Posts:
    10
    Is there any possibility to achieve the same using visual scripting programs?
     
  35. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Hey MinhDao, there are still some things I want to deal with before going into combat, but it shouldn't be too far away!
    Timze, I'm not familiar with visual scripting programs so I'm afraid I couldn't say.

    Here's part eight by the way, it mainly covers setting up checkpoints and loading new levels.
     
  36. MinhDao

    MinhDao

    Joined:
    Oct 28, 2013
    Posts:
    155
    Yeah, new tutorial! I'll watch and do it now. Thank you very much!
    I would like to make a suggestion about ledge grabbing/hanging. I think it very awesome. Can you make the character can grap in the next tutorial?
    Thank you once again!
     
    Last edited: Dec 10, 2013
  37. KageJason

    KageJason

    Joined:
    Dec 10, 2013
    Posts:
    2
    I can't wait for you to get into a combat system, because Currently i have a good idea for a game, and it needs the combat system http://fantendo.wikia.com/wiki/Madnesscrazy's_Adventure


    Once i get this game running good and everything else

    THEN
    i could make it's sequel, but i would need pro to get it all good and going
     
  38. SideEffect

    SideEffect

    Joined:
    Dec 6, 2013
    Posts:
    5
    Thank you for the lessons, it's helped me with trying to create some of my own movement mechanics.

    How would you handle collisions with sloped floors though?
    I can't seem to get it right
     
  39. SideEffect

    SideEffect

    Joined:
    Dec 6, 2013
    Posts:
    5
    I figure probably something with trying to get the normal of the current collision surface and then trying to base movement on that but I don't know
     
  40. SideEffect

    SideEffect

    Joined:
    Dec 6, 2013
    Posts:
    5
    So I figured out how to get the angle of the slope at the point of collision.
    With that I create a new check to see if the player is on a slope or not.

    If the slope is 180 degrees then the ground is flat.
    If not he's on a slope.

    Then I execute the new movement behavior for deltaY.
    The only problem now is I don't know how to create a new behavior to follow along a slope....

    This code is in the
    if(Physics.Raycast(ray, out hit, Mathf.Abs(deltaY) + gap, collisionMask
    block for vertical collisions in your tutorial.

    Code (csharp):
    1.  
    2.                 // Get slope angle below player
    3.                 float slope = Vector3.Angle(ray.direction, hit.normal);
    4.  
    5.                 if (slope != 180){
    6.                     Debug.Log("slope angle: " + Vector3.Angle(ray.direction, hit.normal));
    7.                     onSlope = true;
    8.                     deltaY = ;
    9.                 }
    10.  
    I haven't given up but any ideas would be appreciated
     
  41. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Hi SideEffect, I'm glad you're trying to tackle the problem yourself! I'll try get slopes working for part
     
  42. SideEffect

    SideEffect

    Joined:
    Dec 6, 2013
    Posts:
    5
    Thanks Sebastian. I'm actually getting really close ^^.

    The behavior for deltaY seems to work now.


    Code (csharp):
    1.  
    2.                 float slope = Vector3.Angle(transform.up, hit.normal);      // slope angle
    3.                 float slopeDir = Mathf.Sign (hit.normal.x);             // positive for downhill negative for uphill
    4.                 Debug.Log ("slope: " + slope + "slopeDir: " + slopeDir);
    5.                 if (slope != 0){
    6.                     onSlope = true;
    7.                     deltaY = Mathf.Tan(slope) * deltaX * slopeDir;
    8.                 }
    9.  
    Now when the character is on the slope it locks movement to follow along the slope.

    The only problem I'm having now is setting up the order right cause it conflicts with being grounded or being in the air.
    Also checking this with all 3 rays causes some weird behavior when you get to the edges of your slopes. One ray thinks you're not on a slope while the others still are.

    I think I only want to check with the middle ray going down.

    The other thing is collisions while moving up the slope. the bottom horizontal ray keeps bumping into the slope. Maybe while on the slope we only want to check with the top 2 horizontal rays.

    I could be over complicating that though.

    Thanks a lot Sebastian!
    I've been an artist for awhile and I'm completely new to programming.
    Your tutorials have helped me a lot and put me in the right direction to try and tackle new problems.

    Jesse
     
    Last edited: Dec 16, 2013
  43. WesolyDzieciak

    WesolyDzieciak

    Joined:
    Nov 18, 2013
    Posts:
    2
    I can not add movement to the cube cos assembler does not see "PlayerPhysics" :/ Any clue what should I do in this situation?
     
  44. MinhDao

    MinhDao

    Joined:
    Oct 28, 2013
    Posts:
    155
    Hey bro, where are you? We miss you :rolleyes: . I hope next week you will come back with next tutorial :D
     
  45. SideEffect

    SideEffect

    Joined:
    Dec 6, 2013
    Posts:
    5
    hmmm I'm kinda stumped on the slopes.
    it seems like the gravity always adding seems to cause problems.
    If I take gravity off completely when grounded or on a slope then the character is no longer seen as being grounded after the initial collision.
     
  46. Lakris

    Lakris

    Joined:
    Jan 10, 2014
    Posts:
    17
    Amazing stuff!

    Any tutorial parts on enemies and shooting?
    And jumping through platforms from below.
    Please :)
     
  47. Chev

    Chev

    Joined:
    Dec 5, 2013
    Posts:
    1
    Thaks dude you rock's :D
     
  48. narwhal-ninja

    narwhal-ninja

    Joined:
    Sep 25, 2012
    Posts:
    16
    Sebastian,

    Great videos, just a question. why don't you use the rigid body component to apply gravity and forces on the player?
     
  49. Random-username

    Random-username

    Joined:
    Aug 6, 2012
    Posts:
    145
    Rigidbodies are definitely a much, much easier way to go in terms of collision detection. I just find that the extra control and precision you get from implementing a custom raycast detection is well worth the extra effort. ;)
     
  50. roma8905

    roma8905

    Joined:
    Sep 30, 2012
    Posts:
    2