SLOPE MOVEMENT, SPRINTING & CROUCHING - Unity Tutorial
HTML-код
- Опубликовано: 14 фев 2022
- SLOPE MOVEMENT, SPRINTING & CROUCHING - Unity Tutorial
In this video, I'm going to show you how to further improve my previous player movement controller by adding slope movement, sprinting and crouching. (You can also use your own player controller if you want to)
If this tutorial has helped you in any way, I would really appreciate it if you leave a like in return. And you can also subscribe to this channel if you want to see more tutorials like this one.
Links:
➤ DOWNLOAD the SCRIPT: www.mediafire.com/file/eibwbh...
➤ DOWNLOAD the PROJECT (in the free-code-dave channel): / discord
➤ DON'T CLICK: bit.ly/2VcrDZt
➡️ Get Access to my full MOVEMENT LAB! ➡️
➤ Trailer: • MOVEMENT LAB - Unity P...
🏹 Get Access to my RANGED COMBAT LAB! 🏹
➤ Trailer: • RANGED COMBAT LAB - Un...
Other interesting videos :D
➤ THROWING Grenades, Knives and Other Objects - Unity Tutorial): • THROWING Grenades, Kni...
➤ FULL 3D ENEMY AI in 6 MINUTES! || Unity Tutorial: • FULL 3D ENEMY AI in 6 ...
Support me on Patreon:
➤ / davegamedevelopment
#UnityMovement #FirstPersonMovement #UnityTutorial
If you need help with any of the code shown, just visit the "coding-help" channel on my discord server :D
Also you can find many more movement tutorials on my channel in the "Tutorials!" playlist - I really hope they help you on your further GameDev journey!
And in case you're interested in getting access to my full MOVEMENT LAB, where I combined all of the movement abilities shown in the tutorials and some more, check out the trailer:
➡ MLab Trailer: ruclips.net/video/9_Q9UxK7VF8/видео.html
Also, since I know lot's of you are interested in ranged combat as well - I can gladly announce that I'm about to release my RANGED COMBAT LAB as well:
🏹 RcLab Trailer: ruclips.net/video/j2YplilHjCA/видео.html
Thanks so much for watching, hope this tutorial has helped you!
The crouching speed in the script is being rewritten by the other states,
Yep
For anyone who' might be wondering why the crouch speed is really slow / jittery or barely seems to be moving at all no matter what you set crouchSpeed to, It's not the states or the ground detection. You have to put a frictionless physics material on the ground object or you get the same bug as when moving against a wall with friction and the player "sticks" to it.
@@blacksad23 he left out the "else" on the last one for the crouch - see the other comments - it needs to be if.... else if.... else if..... NOT what we got in the vid which is if... else if.... if....
everyone taking the time to read thru the comments - also - make sure folks: you set your slopes to WhatisGround for gawd's sakes!! doh!
You know, these are the types of tutorials you like to see, straight to the point, easy to follow, but now boringly slow, and explained perfectly. Keep up the good work 👏
Thanks a lot!
@@davegamedevelopment Dave. The slope system make my Transform spin. I'm using cube collision. Even if I change to capsule It's still spinning.
Please pin this comment, for those having issues with going down slopes being bumpy please make sure that the series of if statements in the move player method are else if's and not multiple different if statements. Also in the state Handler method please make sure that they are all else if until the last one which is an else because otherwise your code won't work correctly. This is what caused issues in my code and it took me forever to figure it out. Please make sure that these are correct.
Thank u that fixed all my movement realated issues
you're a saint
small but important difference!
oh my god i didn't think that would be the problem tysm
bro, you are the best
your tutorials are superb! please do more of these, you dive deeper than many of the existing chanels and provide us with great learning opportunities
You are amazing! I love your voice because it makes everything clearer, you don't miss a word 😎Your tutorials are great! Thanks to you and some further additions, now I can manage an advanced on-ship deck movement!
Ay nice I have actually added sprinting myself before you made this so I was proud of myself also, what an awesome tutorial man keep it up.
Thanks a lot!
This has seriously been a god send, thank you for making these videos, I've learned so much in a such a short amount of time and it is very easy to adapt into my own project
Two things:
1. THIS IS AMAZING. I'm going to get everyone I know to watch and subscribe.
2. Thank you comment section for being the answer to my problems!
I liked and saved your every single video. It's so helpful for my summer vacation game projects
Thanks a lot!
Thank you so much! Please keep making these tutorials, they are awesome!!
Glad you like them, I'll keep going!
it took me many hours to figure how to do slope movment but your method is easy ,thanks a lot
Very great tutorial! It helped a lot!
So informative, thanks a lot!
Thank you so much for making stuff in soft a lot easier to understand.
TNice tutorials comnt from you is legendary. Uncomplicated like your video!!!
Thanks so much for leaving a link to the script! that helps so much when tutorials do that so I can read through the original when I have errors.
Absolute legend. Nice one
This is great, thank you!
it worked! thank you so much!!
will get it. Just don't get burnt out. Whenever you need a break, take one.
when i typed in soft soft tutorial i did not expect it to be tNice tutorials good thank you so much aaaaaaa
Damn bro top tier content, I'm sure you had to visit multiple countries to produce a masterpiece like this
Haha thanks a lot!
OMG THANKS SO SO MUCH THIS HELPED!!!
That was great. Thank you.
After long time
Good to see you 👍
:D
This Man is OP (Overpowered) 😁💪
I like your Videos 👍
Thanks a lot!
You really good at explaining thank you
it's finally ti. i'm ready to learn.
we've missed you for so long ❤
:D
You're a legend you have helpped me alot :')
Thanks, glad I could help! :D
Omg thanks very much ! ✨
works perfectly, thank you
Great video, thank you ! :)
Glad you like it!
Thank u for this ❤❤
WOW IT WORKED!!!!! THANKS MAN
Glad I could help!
Bet your soft is absolutely amazing! Never give up
Very helpful..thanks a lot.
ty cuz ive been having a hard ti getting started.
Great video, I'm really liking these tutorials. One thing: Towards the end when you are accounting for on-slope speed control, instead of adding a new bool flag for jumping from slopes, I actually just re-used the 'canJump' flag instead to keep it simpler. Since 'canJump' is only set to false when the player presses the jump bind, and the Jump() method is called right after, you can use it in the SpeedControl() method to only control the speed on the slope when the player has not yet jumped (so when they are still only the slope). It seems to work all the time for me, but my testing wasn't very long so let me know if I missed something. Also have you thought about making videos about using the new Input System and a Character Controller, especially for a third person controller?
Again, thanks for the help!
Glad you liked the tutorial! And sure, if this works feel free to do it this way. Also I recently uploaded a video on third person movement. And thanks for the other ideas, I'll think about them!
Ah I need to add crouching to my game. Nice job! I might take some ideas from your movement code
Thanks :D
have. But, your tutorial was amazing and I have made a semi-decent soft. Thanks!
!thanks dude!
thank u helped me a lot
For those struggling with very weird movement on slopes and not being able to jump on them, try changing the line "grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);" to "grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround);" sp change 0.2f to 0.3f. Atleast thats what fixed it for me
As I said :D I'm following your tutorials now taking your code :D
Haha go ahead ^^
great tutorials man
actual good code, no brackeys asset store nonsense, this is just what i was looking for
thank you so so so much your a great person you helped
Thanks yummy script love it
Genius bro
For people struggling with the slope with high character velocity, you can add more force to "80f" so you don't get launched in the air, but dont put too much or you'll be stuck on the slope which is worse
what exactly did you change? I have the same problem
@@thazeus1754 go in the script and where you can add force "Vector3.down * 80f" you can get this more high, also normally you can fix it also by verifying missing lines in your code :)
change all the Force.Force's to Force.Impulse's and prepare to laugh your ass off
You got my sub
Thanks!
I have followed your last 3 fps movement and the grenade throwing videos. Thankyou so much the way you explain things it is very easy to understand. Will be following the next vids in the series keep it going!! So much to learn.
P.s. Please add movement on stairs currently if I try to climb stair the player gets stuck.
Glad you like my tutorials! :D
And I'll probably do something about stair movement sometime soon, but it's definitely going to be one of the most complicated tutorials ^^
@Hawk If you wat to move on stairs make an invisible slope overlapping them. It makes them go smoother than other ways and its easy to do.
@@davegamedevelopment ummmmm this is taking awhile
Watch this: ruclips.net/video/DrFk5Q_IwG0/видео.html
Works well!! DANKEEE
Very helpful
Welcome Back
Thanks :D
Bro, this tutorial helped me out so much. Can you make a weapon switching tutorial as well? I want to try and finish my project, but I can't figure out how to make your all in one gun script compatible with other weapon switching scripts on RUclips. With that said, your content is absolutely brilliant. Keep it up!!
Thanks a lot! Glad I could help you :D
I kind of did a weapon switching tutorial already, check it out if you want, but it's a bit specific - only for picking up and dropping weapons, not really storing them in an inventory :D
Thanks Bruu
if you dont want to remove gravity on slopes you can add extra downforce while your y velocity is negative on slopes so that you dont fly off of them
great tutorial!!! please make the sliding/wallrunning one
Thanks - Sliding tutorial is going to be uploaded tomorrow :D
Thank you so much for these videos! They're helping me so much!
I was wondering why you were doing Raycasting to detect the ground below the player instead of collision detection? Is it because Raycasting is more computationally efficient, or is it because it's easier to code Raycasting than writing a loop through the collision object array?
Brackeys Leaves, and Dave Arrives, Boys We Got A New One
Haha thanks!
I am not sure why this is not mentioned but the walk speed overrides the crouchspeed since you are still grounded.
The statehandler still says walking even though you are crouching.
Found the issue: My sprint mode had just Else and not Else if.
// Mode - Sprinting
else if(grounded && Input.GetKey(sprintKey))
{
state = MovementState.sprinting;
moveSpeed = sprintSpeed;
}
saved me a bunch of time. Thanks a lot
Thanks
Lol, was just on your channel and then a new video popped up.
Nice :D
if anyone is having issues with the raycast detecting ground that isn't there (ie you're high in the air and still able to jump), go to your mesh collider component, cooking options, and deselect "use fast midphase"
was tearing my hair out for days trying to figure out why my very short raycast was still detecting ground despite my character being high in the air, tried this and it immediately fixed that issue.
edit: you might need to reimport it as well
This does not work. I am still above 1.0 in the air when able to jump. Unless I check convex, I get message: Non-convex Mesh-Collider with non-kinematic Rigidbody is not longer supported since Unity 5. And with convex checked, I can not move up the slope. I also can not crouch through a tunnel.
alternatively, you could use animations for crouching.
you can simply create an animation within the Unity dope sheet animator, move the players collider and model down, resize things if needed to prevent clipping, or pose them as well if they are a rigged model.
this should resolve any temporary floating in mid air, though you could simple make an if-statement and alternative animation for when the player model is not grounded so that they don't suddenly drop downward.
there's always much more easier alternatives, provided you learn about their attributes
The animation way is the way to go I would say for beginners... the way he does it in this video is just so janky
Hey nice video man looking for the grappling gun part also!!
Glad you like it!
finished 2 in one sittig already lol got the sub
Great! :D
If you input left shift and MoveSpeed is still equal to WalkSpeed,Put the sprint mode under the walking mode in the StateHandler function
sometimes, you can help your script by giving it a little nudge - to make sure everything starts in right place.
For instance, to help you might in your void start:
state = MovementState.walk;
to help give it a frame of reference
also, the coding demonstrated in the tutorial for the StateHandler() function is not the best
truthfully, you need every other one after the first as "else if" for it work best
so in your state handler
if...
else if...
else if...
else if...
for some reason, Dave gets really sloppy in the second half of the tutorial overall, after being so thorough and diligent with good coding in the first parts
0:28 sprinting
2:07 crouching
3:46 handling slopes
We like this.
really Nice
Thanks!
I was expecting a flood of among us comments from the thumbnail, glad there wasn't any lol, anyway thanks for this video :)
Haha you know, the game dev community just accepted the default Unity capsule as a player model for first-person games xD
wow, nice
Brackey's back
There will be no need to add downward force on crouch if your player's pivot is at his feet.
This also negates the need to know the player's height before raycasting to check for ground.
And additionally helps placing units/characters in the scene without needed to adjust their Y position so they don't clip through the ground.
but it can be a real b*ch with all the Rigidbody.Addforce stuff etc gravity goin' on - like center of gravity really helps in Physics in Unity. Could you imagine if you r center of gravity was instead placed at your feet? What a DRAG man
@@TakaShitake-rt8bz You can actually set that manually.
And if your rotation is locked anyway, which it is for most character controllers, then the center of gravity doesn't matter at all.
@@MaxIzrin force on crouch is the best way to guarantee you are the "floor" of any low ceiling shaft, duct, tunnel, cave - that's in essence most important especially if you crouch forward and come into a cave that is 3/4 of your height - you stand up - you will clip the ceiling - you need to make sure at all times when in a crouch environment you are at the lowest point of it - to guarantee a raycast up to check ceiling only hits something your full height or lower - anything above is false and you can stand up - and not clip.
@@TakaShitake-rt8bz the issue with adding force down is that u cant do something like crouch jump. if u hold crouch while in air the player gets pushed down constantly
my crouchijng doesnt work, only my croush speed works but not my yscale, i havent done the ray casting so im not sure if taht is the problem. help quick!!!!!!
Quick and gud :D
Awesomeee video, clear steps and straight to results. I have just a little problem, I'm making a fps shooter game so i have a gun attached to my camera so when i crouch everything also shrinks. is there a fix to to that ?
hey! i'm loving the series so far but i have a quick question, when i crouch the transition between standing and crouching is instant which looks like i teleported the camera downwards whereas in your project and videos the camera moves between the standing and crouching positions i don't know how to explain it better but what i mean is in my project the crouching is instant and in yours it's smooth so how do i make it like yours?
Lerp
@@kurtz2491 Where does the lerp go?
Awesome program
Mess with the Jump fource, cooldown, and air multiplier if you are having issues with choppy jumping.
For me Force of 13, cool down of 0.7, and multiplier of 1 work well (this is with gravity set at -30 in project settings)
Nice video. Do you have a tutorial for walking stairs?
most people got sotNice tutorialng from tNice tutorials video. Much love!
Really Great Video. Well explained and easy to implement in own scripts. Could you make a tutorial for a dash move?
Thanks a lot! And yes, I'm planning to do that :D
shit.. i clicked and now im subbed xD
tNice tutorialngs
These tutorials are great, did have an issue with my crouchSpeed not being used whenever i crouched, after i saw that, i noticed that my movement state wasn't being saved. Just had to set the next if statement to else if in the StateHandler!
YES - and read my other comments in here for more helpful hints - there were a few oversights in the tutorial as presented cheers
Only comment that solved my issue, appreciate it!
Thanks I wasnt able to figure it out
Could you make one for stairs? Mine seems te get stuck on them. Thank you
Thank you man! I just started Learning Unity and i wanted to make an FPS game parkour kinda think but like in 2018 or 2019 i tried but failed thanks for this video now im working on the game THANKS -subbed-
This tutorial has made me realize my typing skills are lacking lol.
Well the typing is sped up to fit the audio, but yes, typing fast is equal to increaing your productivity in coding :D
I recommend where it has the bool for the slope and has != 0 to put like 5 or smthn cause your flat ground wont always be completely flat and you dont want slope physics on flat ground so yea
or, oh shit - ya can maybe remember to make sure your slope is whatisGround
love the tutorial! but what is the asset file you use for slope and crouch tests?!? im wanting to use it for basic testing
It's called Probuilder :D
good tutorial, one question about soft recording. How do you do it? lol
Hello Mr.Dave,
I noticed that jump height is slightly inconsistent. First/single jump is normal however if you hold down space and jump multiple times all jumps after the first one are slightly higher. I tested this even on your project in the description and i was having the same issue. I know its barely noticable but when changing the jump force or gravity the difference can be felt quite a bit. I was wondering if there is maybe some way to fix this ? The easiest solution would probably be just to make the player jump once but i like the feature of autojumping. Hope you see this and thank you for all the amazing tutorials :D.
Yes, I noticed that as well. I am going to fix it sometime soon, but so far didn't spend more time on it.
Try setting the rididbody y velocity directly,
so change the line "rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);"
to "rb.velocity = new Vector3(rb.velocity.x, someJumpForce, rb.velocity.z);"
and delete the AddForce line afterwards.
Didn't try this out, but it might work :D
@@davegamedevelopment I've tried it out and it works just fine. Thanks for this ;)
@@sanducosmin7427 No problem, glad it works!
I tried this and i can no longer jump 😭
@@deadlocked85 Still need help ?
hi, thanks for the video, that helps a lot, i have an issue when i goes down a slope, my player bouncing down. i followed the video until the end, and i dont know how to fix it, his bouncing is the same size of the line grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround); if i turn this 0.2f to a higher value, his bouncing comes higher too
Hi I am having two weird issues, one is that when I am crouched my grounded variable becomes false and secondly when I uncrouch it does a weird and slow growth back to normal height
i have a question, while the player is in air what is the move speed set to? cuz in the script its seems that its not assigned to nothing in air but it still works , is it because the player is always on the ground before jumping so its just set the speed before jumping?
Hey, you make such incredible tutorials. Bravo. I will say though that I do have one pretty odd issue with crouching and slopes. When I crouch on a slope and try to move up on it, my character moves right into the slope and not in unison with the slope, and the character can't move towards the slope. Moving down the slope crouched works fine though. I'm not entirely sure how else to solve this apart from disabling crouching while on a slope. Any help with this at all would be great!
I had a similar issue. But it was when you held down space while crouching it would jump earlier than the rigid body hit the ground. Just make a float and whenever you crouch set it to 0.2f and then 0.5f when you dont crouch set the raycast multiplier to whatever you named your float. I named mine "jumpCrouch": grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * jumpCrouch + 0.2f, whatIsGround); ... if(Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight * jumpCrouch + 0.3f)) This should work. sorry for the late reply, hope you figure out your issue
thanks! Works super well.
sadly, for my FPS, the movement needs to be fairly fluid. Two issues.
1. the player movement script already sucks
2. the player can still fly off slopes and won't always "attach" to them going downwards, especially when playing a faster class.
i'm experimenting with adding extra downwards force when entering and leaving slopes, changing the slope detection raycast distance, adding a new ground detection raycast (it's hard to explain why i'm doing that one), etc. hopefully i can figure something out
my player movement script sucks, not yours btw
Hello, I have the pumping movement problem when walking on the slope, but when I move down not up, unfortunately, the solution that you put in the script did not solve the problem. Any suggestions? Thanks
I have the problem of joting when walking down slope as well, I can add one which make so that there is downward force when walking down as well, but that will push you down on slope as well…
maybe try something like this
if(OnSlope() && (rb.velocity.x > 0 || rb.velocity.z > 0)
{
//apply downwards force
}