How to handle slopes in a Unity 2d platformer game

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ต.ค. 2024

ความคิดเห็น • 27

  • @SunnyValleyStudio
    @SunnyValleyStudio  2 ปีที่แล้ว

    There is a better way to modify the shape of a collider (not to rely on the Vertex distance) -> th-cam.com/video/eDOxDJEtE14/w-d-xo.html

  • @johnjohncaban6480
    @johnjohncaban6480 2 หลายเดือนก่อน +1

    Nice tutorial thank you so much!

  • @InternationalCheeseDude
    @InternationalCheeseDude 2 ปีที่แล้ว +1

    I've had trouble with my character falling over on slopes so this is what i needed

  • @ZombieChicken-X
    @ZombieChicken-X 6 หลายเดือนก่อน +1

    what about the speed? My character moves slower when it is attempting to walk across a slope, also I am using sprite shape with edge collider, no tilemap. How can I normalize the speed based on steepness of a slope?

    • @theplaymakerno1
      @theplaymakerno1 5 หลายเดือนก่อน +1

      He didn't cover this. I will try to make a video about it. You will the normal from the Box Cast or the cast that you are doing and then use its perpendicular to start moving the character.

    • @ZombieChicken-X
      @ZombieChicken-X 5 หลายเดือนก่อน +1

      @@theplaymakerno1 I got it to work, I also did some rotation correction to go for a different feel where the player conforms to sloped ground. I’ll still check out your video tho!

    • @theplaymakerno1
      @theplaymakerno1 5 หลายเดือนก่อน

      @@ZombieChicken-X Which method did you use?

  • @Elor504
    @Elor504 3 ปีที่แล้ว +3

    you are my new brackeys

    • @KalponicGames
      @KalponicGames 3 ปีที่แล้ว +3

      He is way better than brackeys. His code structure is much more sophisticated and makes more sense. Although not beginner friendly. You need to know programming already.

    • @Elor504
      @Elor504 3 ปีที่แล้ว

      @@KalponicGames if to be honest i am also thinking like that, even though brackeys is beginner friendly he isnt going into specific details most of the times

  • @carloslecina9029
    @carloslecina9029 3 ปีที่แล้ว +3

    Great video!
    My approach is to Raycast & check for a slope based on the angle(hit.normal.x) then switch my RigidBody to Kinematic or Static.
    But I got into trouble bc if there's no active collision you can't check for enemy bullets against the Player, and so...
    How would you solved that?

    • @SunnyValleyStudio
      @SunnyValleyStudio  3 ปีที่แล้ว +1

      Hey!
      In my solution I never disable the collider. Even if I set the rigidbody to be kinematic my state patter implementation makes *Idle state* change to *GetHit state* meaning that i can reset the rigidbody to be again kinematic to apply on it any sort of force or whatever you want.
      I know how much work it takes to make each different system work together in even a simple game like a 2d platformer. I have worked last year on a similar indie project and we had a lot of issues until I spent 1 full week refactoring the character movement code to a state pattern. It took so long because I neglected code architecture due to it always being "deadline is approaching". When multiple other systems were depending on the character movement it took a lot of time to make sure that my refactoring didn't break anything.
      In a long run its always wiser to spend an additional day to refactore your code before you start adding more features to your game.
      Anyways thanks for watching! Take care!

  • @Azalea23Horde
    @Azalea23Horde 11 หลายเดือนก่อน

    Hello, Sunny Valley Studio. Your tutorial is very helpful. I encountered a bug in playtesting with my platformer is that the enemies can knockback my main character. When I am grounded on slopes, my character is set to kinematic, but when he is knocked back (mid air), I set him to dynamic, and turn to kinematic again when isGrounded. However, this creates an issue that at certain frame of the game, when the player is standing against a wall and is being knocked back, he goes through the wall because he is kinematic.
    Is there a good solution to this issue?

    • @SunnyValleyStudio
      @SunnyValleyStudio  11 หลายเดือนก่อน

      Sorry to hear that. In that case it might be useful to have additional detector to raycast in the direction of the knockback effect and prevent the movement if there is the wall.
      In general to have a full control over the movement (but also having to code every collision check ourselves) it would be best to use kinematic RB only.

  • @Art_Izon
    @Art_Izon 3 ปีที่แล้ว +1

    Are the rigidbodies on the platforms supposed to be Dynamic or Kinematic? Also, whenever I set my character object's rigidbody2D to kinematic, it falls through the platforms...

    • @SunnyValleyStudio
      @SunnyValleyStudio  3 ปีที่แล้ว

      Hey!
      The only thing that needs a RigidBody is what moves in your scene.
      Ground and objects that you just want to move on are usually static colliders.
      Your character might be falling because it has no collider on it. Basic idea is that for the 2 bodies to collide with each other we need both of them to have a collider appropriate to the physics engine (2D and 3D are different!).
      Next for the physics engine to detect the collisions you need a Dynamic RIgidBody2D to move your character. It moves your player in FixedUpdate so unitys Physics2D engine can detect the collision.
      Also just in case in ProjectSettings for Physics 2D you have Layer matrix so in case you have platform and the player on different layers make sure that both layers are enabled to collide with each other.
      I hope it helps!

  • @alxdrksoul
    @alxdrksoul ปีที่แล้ว

    10:18 am I right to assume that you only made your character stay in the running animation instead of fall animation when moving along the slopes? so if you move along the slopes too fast the character is still falling and not sticking to the ground right?

    • @SunnyValleyStudio
      @SunnyValleyStudio  ปีที่แล้ว

      In the project (it is from my 2D platformer course) I use State Pattern to transition between states. This means that we can check any condition in any state. This means that the grounded check will still be detecting if we are grounded or not when in Falling State and it will transition us from the Falling to Idle state. The Idle state next will trigger the Slope State. I guess that if the character was to be flying high enough above the slope and the grounded check would not be triggered it would not stop.
      I hope that it answers your question. Here is the course link if you are interested courses.sunnyvalleystudio.com/p/code-a-2d-platformer-game-in-unity-using-design-patterns

  • @calcite_dragon
    @calcite_dragon 2 ปีที่แล้ว

    Thank you! This was very helpfull.

  • @TheScorpionAly
    @TheScorpionAly 3 ปีที่แล้ว +1

    Thanks for this very nice explanation :') Are you planning to do a tutorial for different player states?

    • @SunnyValleyStudio
      @SunnyValleyStudio  3 ปีที่แล้ว

      Hey! The state pattern is covered in my video course at courses.sunnyvalleystudio.com/ for people who want to learn how to make a full 2d platformer game and to support this channel. I do my best to also include design patterns in my free YT tutorials for everyone to be able to learn them :)

  • @jibarramx
    @jibarramx 2 ปีที่แล้ว +1

    Please add every new video to yours udemy course, like this one, mobile input with complete control. thank you

    • @SunnyValleyStudio
      @SunnyValleyStudio  2 ปีที่แล้ว

      Thanks a lot for reminding me about that! I will do that as soon as possible :)