How to deal with SLOPES! in Unity 2D using built in Physics

  Рет қаралды 80,703

Bardent

Bardent

4 жыл бұрын

Discord Server:
/ discord
Project Files:
Github: github.com/Bardent/Rigidbody2...
Google Drive: drive.google.com/drive/folder...
If you wish to support me more:
/ bardent
---------------------------------------------------------------------------------------------------------------
We are taking a look at how we can get our character to react to the slope of the ground he is walking on. This is a good simple way to handle slopes that does not require custom physics.
---------------------------------------------------------------------------------------------------------------

Пікірлер: 187
@322ss
@322ss 4 жыл бұрын
multiple thumbs up - always nice to see ways to do slope movement!
@raphaelstiep9815
@raphaelstiep9815 Жыл бұрын
Really Great Video. I had to make a lot of Adjustements to the Code to make it properly work, but it's Amazing how well you explained the concept which led me to nearly write all the Code on my own in the end.
@MabsMakes
@MabsMakes 4 жыл бұрын
Awesome!!! This will come in handy for sure! Thanks for all you do Bardent ❤️❤️❤️
@Bardent
@Bardent 4 жыл бұрын
You are so welcome! :D
@hemessfell7747
@hemessfell7747 4 жыл бұрын
Man I was looking forward to it for so long. Really thank you, you nailed it
@Bardent
@Bardent 4 жыл бұрын
Glad you liked it! :D
@MrCaptainbirdman
@MrCaptainbirdman 3 жыл бұрын
Thank you so much for the tutorial and the zipped code! This tutorial helped me overcome slope implementation that's been taking me over a month to figure out. Appreciate the help.
@Bardent
@Bardent 3 жыл бұрын
I'm really happy to hear that :D
@aaroon1993
@aaroon1993 2 жыл бұрын
I LOVE YOU MAN! I switched from regular tiles(without slopes) to an enormous handmade image level system but slopes been a true pain. Now everythings works perfectly
@P0CK3Y110
@P0CK3Y110 3 жыл бұрын
So for anyone who followed the tutorial and did everything he did in the video, but still wouldnt have their characters stick to the slope (especially when walking downwards or up a sharp edge), make sure to change the Ground Check Radius variable until you find a value that fits your needs. Mine was way too small (0.03) and it took me way too long to figure out what I did wrong. Still an absolutely amazing video that shows a concept which is not that easy to grasp when you first read about it. Great work!
@carlos_7x
@carlos_7x 2 жыл бұрын
I'm still dealing with this. I had tried with so many values on GroundCheckRadius but still my player is moving down while on a slope... any hints into that? I know it's been 7 months hahaha. I appreciate your comment and thanks in advance! :D
@P0CK3Y110
@P0CK3Y110 2 жыл бұрын
@@carlos_7x Make sure you draw your Ground Check Radius, so you can actually see if your circle overlaps with the ground. If it does overlap, you know it has nothing to do with the size of the cicle. My guess is you messed up somewhere in between. If you did follow the tutorial precisely you should have your character sticking to the slope.
@icaro740
@icaro740 4 жыл бұрын
Finally I tutorial brave enough to deal with and explain in a easy way slopes. Thank you so much.
@Bardent
@Bardent 4 жыл бұрын
Glad you liked it
@aless219
@aless219 4 жыл бұрын
uff men, estaba buscando esto hace mas de tres semanas, me has salvado :D
@scrillagorilla4524
@scrillagorilla4524 Жыл бұрын
incredible video. your very clear and simple instructions are on point. thank you
@senjos4805
@senjos4805 4 жыл бұрын
Amazing work!!! Thank you for sharing you're hard work with us!!!
@Bardent
@Bardent 4 жыл бұрын
My pleasure! :D
@k1ng_frnk519
@k1ng_frnk519 3 жыл бұрын
Bardent is the king of actually helpful videos
@evanmiller2438
@evanmiller2438 Жыл бұрын
Best tutorial on dealing with slopes and 2D rigidbodies that I've found by far, thank you for saving me a tremendous amount of research and time sir, subscribed!
@volodyaDikiy
@volodyaDikiy Жыл бұрын
I spent a month researching this topic on my own and he explained it and did it in 30 minutes
@PacoBarba
@PacoBarba 3 жыл бұрын
Good video, the operation is very clear. I have used it for my project and a tutorial that I am offering.
@AnnJayStudio
@AnnJayStudio Жыл бұрын
This is best!!! I have been looking for the solution for a while and finally found this video!! Thank you tons!
@CaesarCanto
@CaesarCanto 4 жыл бұрын
Thanks for the help man. This tutorial is exactly what I was looking for
@Bardent
@Bardent 4 жыл бұрын
Glad to hear it!
@passionboy7542
@passionboy7542 3 жыл бұрын
that's the best simply slope video in youtube with rigidbody !! supa nice ;)
@erfanzaker2114
@erfanzaker2114 Жыл бұрын
Thank you very much sir, you helped me a lot and you explained everything clearly and you helped me to solve my jump problem while my character was running on slopes so thanks again
@Bardent
@Bardent Жыл бұрын
Glad it helped
@Fnighty
@Fnighty 4 жыл бұрын
Hey Hendrick! long time no ear about you, i'll hope u are fine sharing with your family at home, thank you for another usefull tutorial of topics that almost no one talk about in KZbin clearly, keep it up!
@Bardent
@Bardent 4 жыл бұрын
Thanks, you too!
@yessir4935
@yessir4935 4 жыл бұрын
Love u man
@evaluator8906
@evaluator8906 3 жыл бұрын
I thought this would be a bit more complicated, but you made it easy to understand. Thanks for your efforts, I'll try to do it with some sort of acceleration/deceleration.
@jannaukoko3390
@jannaukoko3390 2 ай бұрын
Any update?
@evaluator8906
@evaluator8906 2 ай бұрын
@@jannaukoko3390 Well, I did but that was a while ago lmao
@user-lt1gr7xn6g
@user-lt1gr7xn6g Жыл бұрын
Thank you so much for this!
@jno7
@jno7 Жыл бұрын
Such a great Video! I t saved my life. Thank you so much :3
@huntyBot
@huntyBot 3 жыл бұрын
26:34 don't do me like that man
@Bardent
@Bardent 3 жыл бұрын
I'm SO sorry! It ended up being so much louder than I intended it to be :(
@jonasnober7934
@jonasnober7934 3 жыл бұрын
IT FUCKING SCARED ME AS HELL xd i was just like wtf is going on now?!?!??! hahah
@nicholascarson2554
@nicholascarson2554 4 жыл бұрын
Awesome video man! Thanks!
@Bardent
@Bardent 4 жыл бұрын
No problem!
@billynewcomb2485
@billynewcomb2485 3 жыл бұрын
Fixed my Problem!! I was on the right path before this video, but this solved the problem of launching off of the slope and jittering down the slope. I did change the y velocity, when grounded and no slope is detected, to a negative number because it was launching still, but that completely fixed it.
@Thassio0009
@Thassio0009 3 жыл бұрын
Thank you so much for this tutorial, you saved my life!
@Bardent
@Bardent 3 жыл бұрын
You're welcome! :D
@cystudio5155
@cystudio5155 Жыл бұрын
Overall, this system works great, but I had trouble getting it to fall right. If the character was falling next to a fully vertical wall, it would stop moving if you held the directional buttons against the wall. The way I fixed this was by adding the newVelocity to the rigidbody position and multiplying it by the fixed delta time instead of adding it to the velocity.
@victormeas7898
@victormeas7898 2 жыл бұрын
There's a big oversight in this video: you never mention a condition where isOnSlope can become false in SlopeCheckVertical(), ergo: you will always be considered to be on a slope after you get so much as a different angle from your initial normal. What should be done is get the angle, and compare it to a threshhold (if its around vector2.up then you're not on a slope)!
@pedrinkss
@pedrinkss 2 жыл бұрын
Yep, isOnSlope ends up forever "true" after entering a slope. I managed this issue by checking if (slopeDownAngle != 0), then I set isOnSlope = true. On the else statement, I set isOnSlope = false. This implementation worked for me, for now at least. Cheers!
@boltio3543
@boltio3543 11 ай бұрын
@@pedrinkss you just saved my life xdd
@khanhhoangquoc1800
@khanhhoangquoc1800 10 ай бұрын
@@pedrinkss if u do that character would'nt able to stick with ground anymore
@David-Obermiller
@David-Obermiller 7 ай бұрын
This solution doesn't work, either. Does anyone have a fix for this?@@pedrinkss
@Keanine
@Keanine 2 жыл бұрын
I wrote a system similar to this in a project before but I'm not sure where it is, so this is a real timesaver! I think the only adjustment I'll make is to apply gravity in the direction of the negative slope normal rather than changing the physics material
@smittyz200
@smittyz200 4 жыл бұрын
Well done script and explanation, thank you for this.
@Bardent
@Bardent 4 жыл бұрын
Glad it was helpful!
@F3NR1R04
@F3NR1R04 2 ай бұрын
all this time playing with gravity looking at guides telling me to just use math to clamp the character. When all I had to do was make a stick material thank you so much for making me feel so dumb and fixing my issue XD
@risebad
@risebad 2 жыл бұрын
Amazing!
@hardworkerstudio
@hardworkerstudio Жыл бұрын
Oh it very good! Thank you very much! I just know the Physics in Unity engine sometime are so manual way
@Bardent
@Bardent Жыл бұрын
Glad I could help!
@antondbyz3374
@antondbyz3374 3 жыл бұрын
Thank you 👍
@Bardent
@Bardent 3 жыл бұрын
You are welcome :D
@christucker9566
@christucker9566 3 жыл бұрын
The largest issue with this implementation is that you can't benefit from the Physics engines Forces anymore to apply things like Knockback, as you are over-riding the velocity. Instead, you need to calculate the velocity that you want and use forces to keep the player in place. To do this is quite simple.. Vector2 currentVelocity; Vector2 targetVelocity; Vector2 difference = targetVelocity - currentVelocity; Vector2 force = new Vector2(rigidbody.mass * difference.x, rigidbody.mass * difference.y); rigidbody.ApplyForce(force, ForceMode2D.Impulse); This will keep the player in place for the movement, but also allow ApplyForce to work for knockback and other things.
@genasols.r.l4910
@genasols.r.l4910 3 жыл бұрын
Good, a question, the player jumps finishing a slope of 45, do you know how to make him not jump?
@snebbusneb541
@snebbusneb541 2 жыл бұрын
Hey! I know this is a late reply, but where would you put the above code in the player controller script?
@theDarkerSan
@theDarkerSan Жыл бұрын
There's a bug where the walls are treated as slopes and you get stuck. To solve it just check if the "slope" is more than x angle. Like this. if (slopeHitFront) { float slopeFAngle = Vector2.Angle(slopeHitFront.normal, Vector2.up); if (slopeFAngle < 75f) { isOnSlope = true; slopeSideAngle = Vector2.Angle(slopeHitFront.normal, Vector2.up); } } else if (slopeHitBack) { float slopeBackAngle = Vector2.Angle(slopeHitBack.normal, Vector2.up); if(slopeBackAngle < 75f) { isOnSlope = true; slopeSideAngle = Vector2.Angle(slopeHitBack.normal, Vector2.up); } }
@jno7
@jno7 6 ай бұрын
Man thank you so much for pointing this out! I've been trying to fix this bug for hours now ;-;
@killdeer
@killdeer 7 ай бұрын
This was the best slope checking system I could find. Worked excellent but I'm trying to upgrade my project to use the new input system and man did I fruck the code up badly.
@berkcan3475
@berkcan3475 2 жыл бұрын
this is best video thanks for it :thumbs_up:
@Emilis2023
@Emilis2023 Жыл бұрын
In case anyone else is having trouble with this like I was, after a lot of messing around I realized my problem was I was casting a collider out in front of the character to do a wall check then had a condition set to stop all horizontal motion if one was detected to prevent sticking to walls. If I walked towards a slop the bottom of the collider was hitting the edge of the slope and triggering "onWall" so I couldn't move towards it. Turned that off and it worked just fine. Of course now I'll have to get them to work together, but I think it should be a simple onWall && !onSlope or something similar.
@David-Obermiller
@David-Obermiller 7 ай бұрын
I was having the same problem. I had to create seperate layermasks for walls and ground. Not a big deal since I plan on implementing wall slide/jump later and was going to have to do this anyway.
@mintydog06
@mintydog06 9 ай бұрын
Excellent tutorial. You've covered so much, and I'm very grateful! 26:36 that scared the S**T out of me! lol Can you please explain how the Vector2.Angle is getting the angle. it's using hit.normal, that's the green line ins't it? So it's getting an angle between the green line and a vector that points straight up in the air? When I look at this during gameplay the angle doesn't look 45 degrees to me, it looks to be half that. When I use a much shallower angle, I've got it printing out that the angle is 17 degrees. I have a hard time following what's going on unless I can visualise it. I also have a weird bug where my character can jump even after landing on the slope. It seems at some point the player lands, then slopeangle is set to 0, so then canJump is set to true again for one frame, so the player can then jump while on the slope. I can't work out why. From what I can see, when this happens, the red ray points to the left, instead of along the slope
@kremmidis_773
@kremmidis_773 Жыл бұрын
the Raycast doesn't hit for some reason and the Gizmo never shows up, doesn't work for me
@Atomic-Potato
@Atomic-Potato 4 жыл бұрын
Its a pretty good script for slopes, one problem tho, when you land on a slope while holding the a or d buttons the player will slide down the slope even if the slope is under the max slope angle, any idea how to fix this ? UPDATE: i managed to fix it with the help of bardent discord, turns out isJumping doesnt turn to false when i do that because the velocity.y will never become zero which means the conditions are not true for it to switch isJumping back to false. SOLUTION: in the CheckIfCanJump(); add if(isOnSlope) { canJump = true; isJumping = false; } simple fix but took a while to figure out whats wrong
@gelis07
@gelis07 2 жыл бұрын
I know you
@BrianQuesta
@BrianQuesta 4 жыл бұрын
For the two slope checks, SlopeCheckVertical and SlopeCheckHorizontal, what do "Vertical" and "Horizontal" refer to? I know it´s a basic question, but I´m confused why we need both and what do they do differently? Thanks!
@agenttatsu
@agenttatsu 2 жыл бұрын
how do you get that view of the whole document on the far-right in Visual Studio?
@user-ju1em2he7n
@user-ju1em2he7n 4 жыл бұрын
very nice, can you share the final project files?
@LaQuintaPataDebate
@LaQuintaPataDebate 4 жыл бұрын
Thanks for the tuto. One question: platform tuto is done? Best regards!!!!
@Bardent
@Bardent 4 жыл бұрын
I'm glad you like it! It is far from done :D Busy working on it still :)
@LaQuintaPataDebate
@LaQuintaPataDebate 4 жыл бұрын
@@Bardent thanks and best regards from Argentina!!!
@raimuyo
@raimuyo 3 жыл бұрын
Thanks, really helpful video! But, uh... what's the deal with the Sprite Creator? I'm using 2020.2.1f1 and it just doesn't seem to exist. Haven't even found a mention for it anywhere, but it's still a subsection in the manual under "sprites". Guess they just removed it without mentioning it anywhere?
@ETdoFresh
@ETdoFresh 3 жыл бұрын
it's still there, you got to install it via package manager (i ran across this same thing earlier this month)
@raimuyo
@raimuyo 3 жыл бұрын
@@ETdoFresh Is it still called Sprite Creator? All I can find is 2D Sprite and 2D SpriteShape, which seem to have been installed by default. I tried searching with Preview Packages enabled too, but still nothing. Maybe my install got screwed up somehow?
@niyazimustafa
@niyazimustafa 3 жыл бұрын
I use tile map system and I zip more than one while jumping off the one way effoctr platform. Can you help me?
@orangelimesky
@orangelimesky 3 жыл бұрын
My Ray keeps drawing below my player and doesn't even detect the proper normal of the slope. Update: Instead of transform.position - new Vector3(0,collidersize.y/2), for me it worked with cc.bounds.center - new Vector3(0,collidersize.y/2). Is there a reason you used transform.position there for checkPos?
@skkat7336
@skkat7336 Жыл бұрын
Is there any way to get rid of the bounce when using a faster character, this is a really good system but my character will always bounce a bit when i stop moving becuase its going so fast
@chickendilla
@chickendilla 4 жыл бұрын
Hey Bardent! Really enjoying your videos and learning to program in the procces. I have a bit a of problem applying this to a 3d game. Especially inside the SlopeCheckVertical, in this line: "slopeNormalPerp = Vector2.Perpendicular (hit.normal)". Apparently this can't be applied in a 3d enviroment. Any workarouds for this? thanks in advance!
@chickendilla
@chickendilla 4 жыл бұрын
Forgot to mention that its a 2.5d game where yo cant go in the z axis.
@Bardent
@Bardent 4 жыл бұрын
hmmmm... I can see why that function would not work but I am not too sure about a workaround. I can help think though if you wanna message me on discord and we can discuss it :)
@CarloMercadoJudgementProject
@CarloMercadoJudgementProject 4 жыл бұрын
hi, thank you, really nice tutorial, and method for the slope, I have a question, how can I ignore the slope detection when the player is colliding with a jumpthrough object that is using the plaftorm effector 2D. my idea is to put that objects in another layer, but the problem is that the event for isGrounded wont be trigger with that objects, so the player wont be able to jump.
@Bardent
@Bardent 4 жыл бұрын
Hey! Thank you! Hmm I have never used an effector 2D but I assume if you put the effector on another layer and then add that to the whatIsGround layermask, then where you detect slopes.. once you get a hit you check if the layer is Ground or Effector, if it is effector you ignore it and if it is ground you do the slope check :D
@chrislaguna8390
@chrislaguna8390 5 ай бұрын
I ran into the same issue, its actually a problem with the isGrounded logic, not with the slope detection stuff. What *seems* to be working for me is to first do a raycast check UP instead of down from the same checkPos. if you find something, it would be a fallthrough object. If you are in a fallthrough object, when you check for ground by raycasting downwards, don't check for jumpthrough objects. For me I distinguished jumpthrough objects from normal ground with the z dimension. Hope that makes sense!
@clydemarindo3177
@clydemarindo3177 4 жыл бұрын
where do i cast the ray if i use a box collider instead of a capsule collider?
@madrustrel
@madrustrel 2 жыл бұрын
I have the same problem, and I am wondering if I should change all my animations to fit a capsule collider, or if there is an easy way to apply slope movements to a box collider (to me, they seem to be very similar things, but unity doesn't seem to agree)...
@elijahbyrd3370
@elijahbyrd3370 2 жыл бұрын
Im having issue on where when ever the player goes on a slope facing left it slides down? Any idea how to fix this?
@dr.rgbaum6139
@dr.rgbaum6139 4 жыл бұрын
Thx
@Bardent
@Bardent 4 жыл бұрын
Any time :D
@Kopellis
@Kopellis 2 жыл бұрын
I have a problem that i get stuck on wall when i jump and i press against the walls
@Kakakwawa95
@Kakakwawa95 Жыл бұрын
26:35 Jump Scare BGM !
@feasiblepi8373
@feasiblepi8373 2 жыл бұрын
Hi, I'm having a bit of trouble with the SlopeCheck part of the video and was wondering if you have any insight as to what could be causing the problem. The Debug.DrawRay lines show its checking for the slope much lower than my character (typically in the ground). (8:48 in the video is what I'm trying to achieve) .
@skkat7336
@skkat7336 Жыл бұрын
Thats really weird you should make sure that the slope is on the right layer becuase it might be hitting another object especially if ur using a tilemap
@Kopellis
@Kopellis 2 жыл бұрын
i did everything you did but when i press play my player slowly falls down and passes through ground. if i delete the cc = GetComponent () ; colliderSize = cc.size; . it goes back to normal. i have only watched 10 minutes of the video
@GameDevMarek
@GameDevMarek 3 жыл бұрын
With HingeJoint2D acting as a wheel moving your character you may get some nice results without so much coding :)
@brunosouza9809
@brunosouza9809 3 жыл бұрын
Can you explain how did you do it?
@GameDevMarek
@GameDevMarek 3 жыл бұрын
@@brunosouza9809 Your character has a wheel (Circle Collider) rotating with motor speed value of hinge joint. It's similar to setting up a 2D car with two wheel joints. Hinge Joint is connected to player main rigidbody with a bit wider collider than wheel radius, so it wont try to ride on walls.
@brunosouza9809
@brunosouza9809 3 жыл бұрын
@@GameDevMarek Ok, thank you, I'll try it.
@gunbuckybucketman4578
@gunbuckybucketman4578 3 жыл бұрын
So the Horizon check is just casting ray to the Front and Back of the character and check onSlope to true? But what did slopeSideAngle do?
@Bardent
@Bardent 3 жыл бұрын
Hey! So I had to go back through my code to quickly figure this out but the slopeSideAngle part makes it so that... when we run into a slope that is too steep for us, without the slopeSideAngle, the player would walk up it a little bit then start sliding down, but he will only slide down till the ray shooting down detects a normal slope again which would turn on friction. So he would now slide down all the way. So this is the part where we set canWalkOnSlope to false if slopeSideAngle is greater than maxAngle. Hope that helps clear things up :)
@gunbuckybucketman4578
@gunbuckybucketman4578 3 жыл бұрын
@@Bardent Oh, Thank you
@gunbuckybucketman4578
@gunbuckybucketman4578 3 жыл бұрын
@@Bardent Okay, this is what I'm understand. The SlopeCheckHorizontal is only for detect slope for onSlope check and nothing else? No movement stuff in the method right? *edit : Now I get it, because isOnSlope is true, it will force the player to stick on the ground with "rb.velocity.y" in ApplyMovement method. Thank you again, I'm really into this kind of stuff.
@youtuberyoutuber2495
@youtuberyoutuber2495 3 жыл бұрын
Will this whole script still work if our character moves automatically to the left or right (you dont press any key, he moves by itself)?
@Bardent
@Bardent 3 жыл бұрын
Hey! Yes it should still work as long as the character uses the same movement code. So in ApplyMovement I think, instead of multiplying with input direction you can use something else that decides if the character should be moving right or left :)
@mini7279
@mini7279 2 жыл бұрын
This video was great! the script worked very well, however, the character bounces when going down steeper slopes (e.g., 50 degrees), it also occurs at a lesser degree when going down shallower slopes. I tried looking up tutorials on how to stop this, however I've had no luck with any of them. I directly copied and pasted the source code to ensure I did nothing wrong but it still occurs. I was wondering if you know a solution to this?
@bannedaccount586
@bannedaccount586 4 ай бұрын
i know this is old im leaving this for anyone who may have this same issue the bounciness issue can be cause by the sprites physics shape if youre using a tilemap with a tilemap collider alot of "slope" tiles end up by default with a triangle shape with square edges on the top corner of the shape which causes "divots" to appear in the tilemap collider on your slopes which makes the system think the player is occasionally on flat ground while running on a slope
@JohnAndrews_nz
@JohnAndrews_nz 3 жыл бұрын
For me, this gets stuck to edges of drops when the "slope" becomes around 90 degrees. its a cliff edge, but it will just stick my player.
@stasyandr563
@stasyandr563 2 жыл бұрын
have you found the solution? I have the same problem
@raphaelstiep9815
@raphaelstiep9815 Жыл бұрын
@@stasyandr563 Again this is a very late answer. I had the same Problem, and I don't know if this is a very efficient solution. I made a bool isAtWall and checked for it with a maxWallAngle copared only to the RayCastHit Left and Right Angles. I then added an additional if statement in my Movement Function saying that if I am on a slope, which i can't walk on, but im standing at a wall just add my normal xSpeed and on the y keep the rb velocity. If you need more detail pls contact me
@Ironlionm4n
@Ironlionm4n 10 ай бұрын
I think I have solved this by add the following code in the verticalSlopeCheck after the check if (slopeDownAngle != slopeDownAngleOld) code - if(slopeDownAngle == 0 && slopeSideAngle == 90) { isOnSlope = false; }
@afish2281
@afish2281 2 жыл бұрын
The ray is only returning bool. How do I fix?
@ShashankSingh-lc6qc
@ShashankSingh-lc6qc 4 жыл бұрын
What is slope check distance is doing
@edgyzero6305
@edgyzero6305 3 жыл бұрын
My raycasts are showing up just fine, but the problem I'm having is they're extremely thin, and also render behind the character instead of in front. I have no clue how to fix this.
@Bardent
@Bardent 3 жыл бұрын
Hey! That should be fine it happens for me as well. It has more so to do with the part of the code that debugs the line to show it. Seems like it is a couple of steps behind but the ray should be in the correct position :)
@berkenerenusar6607
@berkenerenusar6607 4 жыл бұрын
Well, I want my character to stop smoothly. With your method, my character will stop instantly when I stop moving. How can I achieve what I want?
@gmatheus10
@gmatheus10 4 жыл бұрын
Decrease velocity gradually when the player release movement button
@cryfen495
@cryfen495 3 жыл бұрын
I'm having trouble with this code. The green line raycast only appears when my character jumps. and when I go on top of platforms the green line appears on the ground below me. I followed everything exactly in this video.
@cryfen495
@cryfen495 3 жыл бұрын
I have now also copied the code in its entirety and added it to a different sprite. Drawray is not appearing at all. This does not make any sense
@hugosouza1993
@hugosouza1993 3 жыл бұрын
Got stuck with my debug.drawray, the ray simply gets weird,
@DjTake1One
@DjTake1One 3 жыл бұрын
I can't figure out what's going on, but I've followed this tutorial to the letter and the SlopeCheckHorizontal code to stop the player flying over sharp corners (like at 19:07) just does not work for me, and, in fact, actually makes the problem worse. If I comment out everything in SlopeCheckHorizontal, the player does leave the ground for a moment, but not much, and keeps going at just about the right angle. If I put that code back in, the player just flies clean over sharp corners and doesn't change velocity to go towards the opposite slope at all. Would you possibly be able to help me shed some light on what I might be doing wrong?
@DjTake1One
@DjTake1One 3 жыл бұрын
I ended up just rewriting the whole thing, but some things in your tutorial still came in handy, anyway, so thank you.
@Bardent
@Bardent 3 жыл бұрын
Glad it worked. Sorry I could not help
@mediocrecreampuff3847
@mediocrecreampuff3847 3 жыл бұрын
I am having some trouble sometimes the character will get stuck in 90 degree corners. Does anybody have a fix for this.
@rafcon9141
@rafcon9141 3 жыл бұрын
same here. Did you fixed it?
@JohnAndrews_nz
@JohnAndrews_nz 3 жыл бұрын
@@rafcon9141 glad i'm not the only one. Disappointing now one seems to have a solution yet.
@Ironlionm4n
@Ironlionm4n 10 ай бұрын
@@JohnAndrews_nz I fixed this by changing the VerticalSlopeCheck funtion code - private void VerticalSlopeCheck(Vector2 checkPosition) { var hit = Physics2D.Raycast(checkPosition, Vector2.down, slopeCheckDistance, groundLayerMask); if (hit) { _slopeNormalPerpendicular = Vector2.Perpendicular(hit.normal).normalized; _slopeDownAngle = Vector2.Angle(hit.normal, Vector2.up); if (_slopeDownAngle != _slopeDownAngleOld) { _isOnSlope = true; } if (_slopeDownAngle == 0 && _slopeSideAngle == 90) { _isOnSlope = false; } _slopeDownAngleOld = _slopeDownAngle; } if (_slopeDownAngle > maxSlopeAngle || _slopeSideAngle > maxSlopeAngle) { _canWalkOnSlope = false; } else { _canWalkOnSlope = true; } if (_isOnSlope && _xInput == 0 && _canWalkOnSlope) { _rigidbody.sharedMaterial = fullFriction; } else { _rigidbody.sharedMaterial = noFriction; } }
@rafcon9141
@rafcon9141 3 жыл бұрын
How could rotate my player so it aligns to the ground?
@Bardent
@Bardent 3 жыл бұрын
Hey! So to do this you can use the normal that we find for the slope and then rotate the sprite to match that. I would not rotate the base character though as that will mess with the slope detection
@BRtn-nn9nr
@BRtn-nn9nr 2 жыл бұрын
wait.. the slopeSideAngle is unused ?
@longuemire748
@longuemire748 2 жыл бұрын
Excuse me, but is it adaptable for 3d?
@ercantoka
@ercantoka 3 жыл бұрын
Hi, How do I use a SpriteRenderer. For character animation. Help Me
@Bardent
@Bardent 3 жыл бұрын
Hi! You can check my main series to see how I set up my animator :)
@ercantoka
@ercantoka 3 жыл бұрын
@@Bardent thank you. I've been reviewing your video since yesterday
@drunkengaming4782
@drunkengaming4782 2 жыл бұрын
On Unity 2D there is no option to Create > Sprite, only Create > 2D > Sprite but this doesn't include a Polygon for some reason? EDIT: You can download the project and take the polygons from the assest folder to your own project. Any idea why the Debug.DrawRay isn't showing for me? I enable Gizmos with everything on and adust the slopeCheckDistance.. hmmm..
@Ironlionm4n
@Ironlionm4n 10 ай бұрын
You can still create a Polygon sprite, you just create a Square sprite, then change its Sprite Mode to Polygon and then you can change the sides and outlines in the Sprite Editor
@ScrewY0UguyS
@ScrewY0UguyS 3 жыл бұрын
How can I find polygon sprite? Don't see it anywhere.
@felipecardoso9703
@felipecardoso9703 3 жыл бұрын
Create a Square Sprite Select it Go to Inspector and change Sprite Mode to Polygon Apply changes Open Sprite Editor and you can edit it
@theOrdinaryDev
@theOrdinaryDev 3 жыл бұрын
my hit.normal is messed up. it draws a ray at a weird angle.... pls HELP!!!
@dreadzitodev488
@dreadzitodev488 3 жыл бұрын
that is because you put hit.normal in the Debug.DrawRay function hahaha I made the same issue and I got crazy, just replace it to hit.point
@br-zhou
@br-zhou 2 жыл бұрын
26:35 why you jump scare me like that
@bigboobmasterbaiter69
@bigboobmasterbaiter69 3 жыл бұрын
26:33 you scared me good, i was very concentrated.
@Bardent
@Bardent 3 жыл бұрын
So sorry about that! It ended up being so much louder than I intended
@joeysimon3331
@joeysimon3331 2 жыл бұрын
For some reason mine still slides down slopes :( EDIT: Finally figured it out. It's because I had a Physics material applied to my collider as well. I just removed the Physics material from the collider and it works fine now :)
@joshuaoreskovich9342
@joshuaoreskovich9342 4 жыл бұрын
there are 2 problems with this code. 1) You stick to walls if you press up against them. 2) there is a pause coming on and off slopes.
@Bardent
@Bardent 4 жыл бұрын
Hey! Okay so for 1 that is my bad I totally didn't even consider straight walls XD But you can fix that by using a shorted slope check distance for the horizontal rays. I used 0.2 and that made it so I am no longer stuck. You will need to live the vertical one as is tho. As for 2 it does not seem to be happening on my side but I do remember something like that happening during development. Can't remember what I did to fix it though as it's been a while. If I remember i'll let you know.
@matheusreidopedaco
@matheusreidopedaco 3 жыл бұрын
Can this work with AddForce? How?
@stellarestuary589
@stellarestuary589 4 жыл бұрын
I was close to getting something working but there are a few things happening with my game: 1. The character is still sliding down the slope. Furthermore the 2D Raycast is not shoing up for my character. 2. While the character does still slide, however, the code I used from your video managed to fix an issue where the jump animation when facing walls would continue to play upon landing back on the ground. So the thing about my code is that it uses a script that I wrote that is actually a 2D Player Controller from Lost Relic Games's Mega-man style tutorial video, so I've expected something like this to happen. I've tried adding a physics material to the slope as well to stop the player from sliding with no luck. Am I missing information that I should look into before following your video? I actually don't understand some of the logic here, so I'm thinking of putting this problem on hold for now. This is the video I followed that I made my script from. kzbin.info/www/bejne/amXHm6SLicxlick
@Bardent
@Bardent 4 жыл бұрын
Hey! Hmm there is a lot of things that could be an issue. Slopes are really complicated which is why I also have not attempted adding them to my main series. I just wanted to share what I managed to figure out. If you want to hop on discord I can help you debug what the issue is with yours :)
@stellarestuary589
@stellarestuary589 4 жыл бұрын
@@Bardent For what I need, I think you figured out it out perfectly. I'll see if I can pay you a visit and show you what I've tried so far.
@dr.rgbaum6139
@dr.rgbaum6139 4 жыл бұрын
Hey can someone help me what does he mean with:whatIsGround. ?
@Bardent
@Bardent 4 жыл бұрын
Hey! whatIsGround refers to a layermask that we use with the raycast so it only detects objects on the same layer. So basically our ground is on a Ground layer and the physics check only checks that layer.
@dr.rgbaum6139
@dr.rgbaum6139 4 жыл бұрын
Ah okay thx
@Chief-wx1fj
@Chief-wx1fj 3 жыл бұрын
Hi this works really good, but if my slope gets too steep (an angle higher than for eg 50), the "maxSlopeAngle" doesn't matter anymore, the character starts sliding up. Anyone else has the same issue? btw this only happens when the slope is tilted right (positive value) EDIT: I found the my issue for anyone with similar problems. My "isGrounded" was set up differently than his. I increased my grounded radius and it fixed the problem. If it doesn't work try to increase the Slope Check Distance too.
@Bardent
@Bardent 3 жыл бұрын
Thanks for sharing your fix!
@quantdev
@quantdev 4 жыл бұрын
how to make loops de loops like in sonic
@Bardent
@Bardent 4 жыл бұрын
oof I'm gonna need a while before I can figure that one out
@prabhasinipradhan8868
@prabhasinipradhan8868 3 жыл бұрын
I wanna make mobile control joustick linked to the character
@hiiambarney4489
@hiiambarney4489 4 жыл бұрын
hmm sadly, nothing of this works for me at all. Messed up my entire code and wasted 30 minutes of dev time for my jam. So this is my perspective, could be I'm doing something wrong but I was trying to modify the character controller from Bardents other series, not sure why he didn't use that as I am very sure it's a high% use case for regulars on his channel.
@Bardent
@Bardent 4 жыл бұрын
Hey I'm sorry this did not work for you. Slopes with built in physics is definitely a little finicky and take a lot of testing and tweaking of values to get it to work right. That is the reason why I am not yet including this in my main project as I would rather spend that time working on other features. Hope you managed to get everything working for your jam though. Good luck!
@hiiambarney4489
@hiiambarney4489 4 жыл бұрын
@@Bardent It's ok man. I spent a day rewriting an entire raycast based platformer physics system before I noticed my tilemap collider put whacky dents in my obviously flat surface sprites for no reason. An apology is due. Sorry man! It can be frustrating sometimes working with Unity 2d
@design6462
@design6462 4 жыл бұрын
hi im from Your followers on youtube, can you help me , i have problem. about How to create mobile games for different screen sizes and resolutions. im wait your answer, thank you so much
@Bardent
@Bardent 4 жыл бұрын
Hey! Unfortunately my experience with that is very limited but I can recommend a video called "Handling different aspect ratios in your Unity 2D games" I hope that has what you are looking for :D
@design6462
@design6462 4 жыл бұрын
@@Bardent Thanks
@JackBz
@JackBz 2 жыл бұрын
There's some definite issues I'm having with this. Even in your own project, the player gets consistently stuck whenever it goes against a totally upright wall, only being able to free itself by jumping
@JackBz
@JackBz 2 жыл бұрын
this is if the wall is totally upright and floor is totally flat
@danmorin5894
@danmorin5894 2 жыл бұрын
I ran into this same issue. I resolved it by adding 'if (Vector2.Angle(slopeHitFront.normal, Vector2.up) < wallAngle)' around the two places where slopeSideAngle and isOnSlope are set in the Horizontal Slope Check. wallAngle is some float that describes a slope so steep it is a wall. I used 87
@crump05
@crump05 4 жыл бұрын
for some reason my drawrays are not visible
@fatmarajab114
@fatmarajab114 4 жыл бұрын
i have the same problem... Did you figure it out??
@Bardent
@Bardent 4 жыл бұрын
Hey! In the game view make sure you click on the Gizmos button at the top right of the window to toggle it on and off. This can be done in the scene view also :)
@stefan429
@stefan429 2 жыл бұрын
the random xfiles tone xD wtf
@Bardent
@Bardent 2 жыл бұрын
It's peak comedy mixed with really bad editing
@stefan429
@stefan429 2 жыл бұрын
@@Bardent the video was great tho, works perfectly for my game when no other controller/video could help!
@nagybalint1474
@nagybalint1474 3 жыл бұрын
TODO: DOTHIS
@ashtimbo
@ashtimbo Жыл бұрын
20:15
@pulsar1934
@pulsar1934 2 жыл бұрын
It doesn't work slope is always false
@Monagle0
@Monagle0 2 жыл бұрын
Great, yet another Tutorial video I can't watch beacuse of a irritatingly loud keyboard
@Ironlionm4n
@Ironlionm4n 10 ай бұрын
Hey I have the same problem, you may have the same disorder I have called Misophonia. Many people suffer with this disorder without knowing it exists.
Why Stairs Suck in Games... and why they don't have to
11:24
Nick Maltbie
Рет қаралды 1,5 МЛН
Improve Your Platformer with Forces | Examples in Unity
7:01
Dawnosaur
Рет қаралды 164 М.
Indian sharing by Secret Vlog #shorts
00:13
Secret Vlog
Рет қаралды 59 МЛН
Bro be careful where you drop the ball  #learnfromkhaby  #comedy
00:19
Khaby. Lame
Рет қаралды 51 МЛН
100❤️ #shorts #construction #mizumayuuki
00:18
MY💝No War🤝
Рет қаралды 20 МЛН
[Unity] Creating a 2D Platformer (E04. climbing slopes)
18:47
Sebastian Lague
Рет қаралды 124 М.
Better Jumping in Unity With Four Lines of Code
12:47
Board To Bits Games
Рет қаралды 817 М.
5 Reasons Your Indie Platformer Game Sucks! (+Easy Fix!)
6:39
Jonas Tyroller
Рет қаралды 232 М.
Escaping Unity Animator HELL
18:18
Lost Relic Games
Рет қаралды 493 М.
Secrets to a Great Platformer Character with Unity
3:11
Dawnosaur
Рет қаралды 53 М.
Making a Background BEAUTIFUL (My full process)
8:01
Lost Relic Games
Рет қаралды 93 М.
Indian sharing by Secret Vlog #shorts
00:13
Secret Vlog
Рет қаралды 59 МЛН