Creating 2D Games in Unity 4.5 #11 - Character Controller #7 - Slopes - cybergame-beauchamp.fr

Creating 2D Games in Unity 4.5 #11 – Character Controller #7 – Slopes

3DBuzz
Views: 17747
Like: 79
Although the game we designed doesn’t have any slopes in its levels, our character controller wouldn’t be complete if we didn’t handle moving up and down angled platforms.

Assets:

Full series:
(youtube)
(3dbuzz)

———————————————————————————————–
This series shows the entire process of creating a fully featured, polished, 2D game in Unity 4.5! We show many of Unity’s new 2D features, such as the sprite renderer and editor – as well as animation, sound, custom character controllers, and more! At the end of the series, we wrap up by constructing two fully featured levels for our game.

This series expects a beginner-level understanding of C# as well as a very basic understanding of Unity’s interface. However, people completely new to Unity can use this series as an accelerated introduction to the beginner to intermediate features of the engine.

Learn more about the MMO at:

20 Comments

  1. I have not watched this video but I have enough from the last video to get my character moving. Thanks, my character doesn't move like claymation anymore.

  2. How can we rotate the player to match the slope angle and slide down slopes that are very steep.

  3. How would I make the player move up slopes at the same speed as moving down? With the code I have now the difference in speed between going up and going down is really huge unless I make the slope angle so small its barely a slope anymore.

  4. When my player tries to move up a slope, he cannot, even if I jack up the slope limit to 90. He actually slides down the slope (opposite the direction I am trying to get him to move). I tried looking through the code and comparing with yours, but found nothing that would be causing this… any thoughts? Here is my CharacterController2D code (https://gist.github.com/anonymous/66af1f2bc24b5b4451e8).

    Thanks!

  5. I think you should explain in a couple English sentences what each of your methods is generally going to do before you start implementing them.  In math, there is no concept of a "vertical" or a "horizontal" slope.  So that leads me to think handleVerticalSlope will handle movement in the y direction when moving along a slope and handleHorizontalSlope will handle movement in the x direction.  But that's not the case either because handleVerticalSlope only handles movement if the player is moving down a slope, not up a slope.  And handleHorizontalSlope handles both movement in the x and y direction.  And why name a variable "slopeDistance" if it has nothing to do with the distance of the slope or even the distance to a slope.  I think "distanceOfSlopeDetector" would have been more appropriate even if it is a little longer.  In many chunks of your videos you just type the code while reading it out loud without explaining on a high level what it's doing. For example you could have explained that the reason you need a ray casted so far down from player's feet is because you're not rotating the player with the slope.  And the distance of the ray increases as the player scales up because the distance from his feet to the slope increases.  When you don't explain the code and just say it out loud, it's equivalent to your viewers looking up code examples, except the code isn't even commented.  Don't take this the wrong way, anyone can tell your an amazing programmer by the way you encapsulate and abstract different parts of the code, but you could definitely improve in the names you come up with for methods/variables and explain the significance of each line of code that is not immediately obvious.  

  6. So how do I fix the collision spam when going up a slope? Or is that discussed in a later video?

  7. @3DBuzz Can you go over why the character can't go up slopes or jump after going up a slope? It seems like this is a fairly common topic in the comments. 

  8. I HAVE  the same problem here , my player can't walk on slopes even if i set my slopeLimit angle to 90, Please help NELLLLLLLSON , where are you ??

  9. Hey guys I figured out what was going on (at least in my case) and I'd imagine something similar is happening for at least some of you. So I'll post my code below for you but before you even worry about changing your code one thing this video doesn't explicitly tell you is that your characters Slope Limit is controlled by your character controller2D. You may remember giving your Slope Limit a range of 30-90 with a default of 30 in the character controller video, which is fine if all your slopes are 30 degrees or less but you will run into problems when you hit any slope higher than 30. Changing your characters Slope Limit requires NO code at all. You already wrote that bit of code in your character controller2D. So in unity click on your character prefab, in the inspector you'll find your character controller2D and you'll see a slider that says Slope Limit and it'll be set to 30, you can mess with it to set the max slope angle that your character can travel. This may be enough to fix your issues. If it's still not working than there may be something wrong with your code. I provided my own below but keep in mind even with my code, you still need to make your Slope Limit higher than 30, so do that first, it may be the only thing holding you back anyways.

    Your code should look a bit like this for reference. If you're gonna copy and paste this into your own project I put a comment in private bool HandleHorizontalSlope to make it so you move up and down slopes at regular speed. It was a personal taste thing, the line is fully functional so feel free to un-comment it for that functionality. 

    private void HandleVerticalSlope(ref Vector2 deltaMovement)
    {
    var center = (_raycastBottomLeft.x + _raycastBottomRight.x) / 2;
    var direction = -Vector2.up;
    var slopeDistance = SlopeLimitTangent * (_raycastBottomRight.x – center);
    var slopeRayVector = new Vector2 (center, _raycastBottomLeft.y);

    Debug.DrawRay (slopeRayVector, direction * slopeDistance, Color.yellow);

    var rayCastHit = Physics2D.Raycast (slopeRayVector, direction, slopeDistance, PlatformMask);
    if (!rayCastHit)
    return;

    var isMovingDownSlope = Mathf.Sign (rayCastHit.normal.x) == Mathf.Sign(deltaMovement.x);
    if (!isMovingDownSlope)
    return;

    var angle = Vector2.Angle (rayCastHit.normal, Vector2.up);
    if (Mathf.Abs (angle) < .0001f)
    return;
    State.IsMovingDownSlope = true;
    State.SlopeAngle = angle;
    deltaMovement.y = rayCastHit.point.y – slopeRayVector.y;
     
    }
    private bool HandleHorizontalSlope(ref Vector2 deltaMovement, float angle, bool IsGoingRight)
    {
    if (Mathf.RoundToInt (angle) == 90)
    return false;
    if (angle > Parameters.SlopeLimit) {
    deltaMovement.x = 0;
    return true;
    }
    if (deltaMovement.y > .07f)
    return true;
    //The line of code below that I commented out makes it so that you move slower up slopes, and faster down slopes.
    //I chose to comment it out because I prefer for my character to move up and down slopes at normal speed.
    //If you want to turn that line of code back "on", delete the "//" before the "deltaMovement.x" you see below.
    //deltaMovement.x += IsGoingRight ? -skinwidth : skinwidth;
    deltaMovement.y = Mathf.Abs (Mathf.Tan (angle * Mathf.Deg2Rad) * deltaMovement.x);
    State.IsMovingUpSlope = true;
    State.IsCollidingBelow = true;
    return true;
    }

  10. if any body have working script plazzz z zzz zz game me, i try lots of time it's not work. it will be thank full.

  11. HandleHorizontalSlope should really be called "GoUpSlope" and HandleVerticalSlope should be called "GoDownSlope". The naming is quite confusing since a slope is both vertical and horizontal. I like you guys a lot, but if you took a little time to explain what the function is going to do before writing it, it would help clarify a lot. 

  12. I am having trouble going up the slopes too; my character is jittery and once in a while bumps down the slope instead of up, even while holding the opposite key. 

    I assume the issue has to do with the way rays are being cast, perhaps the horizontal ones. I'll spend some time trying to figure it out on my own, but failing at that I'll try asking on the 3DBuzz site and Unity Answers.

    EDIT: Doing away with "break" under "if (i == 0 && HandleHorizontalSlope…" seems to smooth out the movement going up because that stops the vertical rays being cast. (something like that)

  13. So the jjittery motion going up slopes, what is the cause of this with this solution? How can I "smooth" the movement?

  14. private bool HandleHorizontalSlope(ref Vector2 deltaMovement, float angle, bool isGoingRight)
     {
     
      …
       // if (deltaMovement.y > 0.07f)
       if (_jumpIn > 0) // If the player is jumping dont change deltaMovement.y after this line so the player would be able to jump during running up the slope
        return true;

  15. Hello! on my project I get this error in this script.

    CharacterController2D.cs (332.23): error CS0029: Can not implicitly convert type float 'to bool' . How could I solve this problem?

  16. For those of you having the following issues:
    -character goes up slopes slower then going down slopes
    -character doesn't go up steep slope even with slope limit set to 90

    try this solution which worked for me:

    Comment out or remove the line below in the HandleHorizontalSlope method:

    deltaMovement.x += isGoingRight ? – SkinWidth : SkinWidth;

  17. Collision above isnt checked while moving up a slope

  18. private static readonly float slopeLimitTangant = Mathf.Tan (75f * Mathf.Deg2Rad);
    &
    var slopeDistance = slopeLimitTangant * (_raycastBottomRight.x – center);

    can anyone explain to me what this code is doing. I'm trying to get the most out of these tutorials and these lines were breezed over like they were simple. I just watched 2 hours worth of Trig videos and don't feel any closer to understanding what is going on here.

  19. Hey guys! First id like to say that your videos are outstanding, i love how how you explain every detail! I actually own a C++ DVD i bought years ago from you guys:)

    Now for my problem. When my player hits either a slope or a moving platform (from the left or right side of the player), his physics go wacky. So I'm jumping at a normal height and when this wackyness happens, i can only jump a small fraction of the height i was jumping at previously. Any idea whats happening here?

Leave a Reply

Your email address will not be published.