- Home /
2d Sidescroll problem with wall sliding
So I'm currently making a 2d sidescroll and I found a problem, my character remained "stuck" with a vertical wall when I jumped to it and was not sliding down to the floor. I looked up on the internet and "fixed" it by using physics 2d material and now the character slid, but now as the material affects the whole collider, the character slides in the whole platform, not only in the sides. So I used two box colliders, one for the upper part of the platform, and other that occupies the rest of the platform, but that ended with my character getting stuck in the corner of the platforms (I did the same but with the character). So my question is: There is way to affect only some sides of a collider (box collider 2d) with a physics material? Or there is a way to fix my problem by code, such as "behaviorWithWalls"?
Don't know if relevant but I'm using rigidbody2d.velocity to move my character and making it jump.
Answer by SnStarr · Jan 10, 2015 at 06:37 AM
If you're using raycast functions for your player to detect collisions within 2d space, like raycasting above and below him to detect colliders then the common problem would be that you need to also have raycast not only check above and below the player for collisions, but also left and right. If its because he is getting stuck on "corners" or objects with box colliders have the raycast check at an angle in the direction he's moving. Like after you jump while moving forward and hes falling, the raycast needs to check the combination of his forward vector and his downward vector. You can do this by usind a For Loop with a Lerp amount on a Vector 3. Or else he will get stuck on box collider corners. If I read your op wrong, im sry try this tutorial
use this tutorial. http://deranged-hermit.blogspot.com/2014/01/2d-platformer-collision-detection-with.html
TY, and how do I do with cliffs? so when the player is trying to walk in a cliff he doesn't just slide... Also, I can do the same you explained but with linecast? because I think I don't understand that much about raycast...
Yes I meant LineCast...I always just say raycast but forget to call it line casting when talking about 2D. Line casts should be cast in 10 directions to detect collisions at all times on a 2D surface. Similar to something like this. Although I think 3 lines cast from bottom like, 1 on each edge and 1 in the center. And three at the top. One on each side and then one in each corner. Can be done in a single function im pretty sure. Sorry, I am more designer than coder, and I am sure a coder will come along and tell me im wrong or right.
Now that I think about it, You could prolly use one LineCast and just have it cast in the velocity the play is moving. $$anonymous$$eaning if he is falling & moving right, then the line cast would cast from the bottom right corner. If he was jumping and walking left it would cast from the top left and well, you get the picture. Literally above.
Yeah I get it, but what I'm supposed to do with the information when the player detect nearby walls, I mean how the player have to "react" to prevent stucking with walls? The only thing I have on $$anonymous$$d (because of my very basic knowledge) is to stop the player by a Vector2.zero (on the rigidbody.velocity), but that would just freeze the player until he reachs the floor.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180013im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name `CrossPlatformInput' does not exist in the current context 3 Answers
The name `PlatformerCharacter2D' does not exist in the current context 1 Answer
How do I get the character to jump in a 2d game, which is compatible with joysticks and keyboards? 0 Answers
2D moving platforms, physics issues 1 Answer
2D Character won't jump diagonally 1 Answer