Godot Advanced FPS Movement - Crouching Tutorial In 5 Minutes
Вставка
- Опубліковано 30 лип 2024
- -- Social --
Discord: the discord server has been shut down, sorry!
Twitter: / garbaj2
-- Links --
Github: github.com/GarbajYT
-- About --
Following up on our basic first person shooter character controller tutorial, this video will be the first installment in a new advanced FPS movement series where we dive deeper into more complex movement mechanics. In this video, I'll be showing you how to make your FPS character crouch. A simple ability that is not so simple to implement. - Ігри
username: garbaj
video quality: not garbaj
Hehe, thanks
Saw that myself - name is Lol quality is O.O
Agreed.
An alternative approach for crouching that some may find useful: Set the default height of the capsule to be the crouched height, and then make the head a collision sphere with the same radius as the capsule instead of a Spatial node. Then you just have to move the head down enough that the sphere will be inside the capsule when you crouch. There should still be enough overlap when standing that you won't have gaps between the head and body that bullets can pass through. And as an added bonus you'll essentially get headshot detection for free, since the head will already have its own collision.
btw: for the sticking to the ceiling problem you can actually use the _KinematicBody_ _is_on_ceiling()_ method that works the same as _is_on_floor()_ but, you guessed it, for ceilings.
Thanks for the tip!
@@garbaj you thanks for the excellent tutorials, they are just what I needed to get started with godot!
Before the adjusting to the environment part, I was wondering why my dude was clipping through the obstacle. Then you covered that. It's very cool that it adjusts according to the ceiling height! Thanks!
Thanks, I'm glad you found it helpful
that was intense, i thought for sure id catapult somewhere
Keep going man, good work, good tutorail, and good luck for your project!!!
Bippity, bappity, bop... done! Nice!
I would add if "Input.is_action_pressed("crouch") or is_bonked" to replace "if Input.is_action_pressed("crouch")", so if you are stuck in a place like in csgo, when going to mid window from ct spawn, there is that little entrance where 4:25 is applying. The player will still be slowed down when crouching, not like before when you let go of ctrl you can run as a croucing player :)
Thank you for this comment
THe way you do it changed so now its - if Input.is_action_pressed("crouch") or head_bonked:
excellent job, thank you again
Nice tutorial! It could be cool if you download some character from Maximo and set all animations and model with your code
That’s actually a good idea, I’ll put it on the list
I know this is a fairly old video now but I thought I'd post this up in case people were having the same problems as me, two things I would warn about this player controller but they're fairly minor annoyances that you need to work through. If people are having issues with crouch 'doing nothing' double check the height your player is set at and increase it if the height is lower than the height of the player capsule there will be only a small movement and the camera and capsule won't shrink correctly. I also noticed a bug with this controller where if you try to make a vent to jump through like in the old Half Life games and crouch into it when you release the crouch button inside the vent the player and I think it's because the controller still thinks somehow it's in midair so it defaults to player 'falling' and using gravity so it gets stuck in the floor because it seems to unshrink itself.
Anyone got any ideas? I might suddenly fix this problem but we'll see, will post an update if I do.
Edit: Okay I just figured it out and the problems are to do with the height of the obstacle you're ducking under when you release the crouch action if you decrease the height of the obstacle above you then the player sinks into the ground so you need to experiment and match the height difference with whatever settings you've chosen. When placing the obstacle matching the height difference to the size of the capsule seems to fix it, after that you've just got to scale everything to make it all look right like if you were doing what I wanted to do and make a vent to crawl through for example.
ok, so im slowly learning gdscript and i have started changing your code while watching tutorials for my ease. lets see how this works haha
4:05 im using the improved tutorial which uses gravity_vec
is there like any other way to replace fall.y
I did this in a more convoluted way, but I gotta say it works like a charm.
- I used two collision shapes, one high and one short, and I alternate between both when I crouch/uncrouch.
- I have an AnimationPlayer to move the camera, both for crouching and for peeking around corners. This lets me control the movement curve, to do some easing while still keeping the movement quick and snappy.
- I also use 5 head bonker rays, one in the center and one in each corner (very slightly offset inwards), but only for preventing uncrouching when crouching under low ceilings (I never had the problem of getting stuck on them). So you can let go of the crouch key when you go into a small tunnel, and then it will uncrouch automatically when you get out of the tunnel (my *stand_up()* function checks the rays for collisions).
That's a pretty clever solution! There's always more than one way to do anything in programming
@@garbaj Indeed. My initial thoughts were to do it kinda like you did it, but I had some trouble with it, and also I didn't quite know how to move the camera gradually over time back then... So I had to come up with something else.
Now I only have to figure out how to also add a view bobbing animation, as well as how to fix the peeking ones, because they don't seem to all play along nicely. I suspect I may have to either implement some sort of state machine, or use the AnimationTreePlayer (or whatever its called), or use a second animation player, which also should work.
@@skaruts teach me how to do your crouch system, pls
Yep, I think garbaj just made an inconsistent video, in this tutorial he references the 'CollisionShape' which used to be a node called body and the collision shape you're change the height of is 1.5 whereas the body in the previous video has a height of 3. Just something to watch out for, wonder if he edited the video awkwardly and didn't double check things because all of a sudden when I change my settings to match what he's done in this video it works better.
I have an error,"Attempt to call function 'is_colliding' in base 'null instance' on a null instance" when i try to get the raycasting node
Y like your videos bro I think they re really helpful fr the community, you should do how to put a caracter and a weapone for the next one
Coming soon ;)
I'd love a tutorial on a dynamic 3rd person camera. An over-the-shoulder view, You know, that zooms in when you walk, and out when you run / when enemies are nearby
I was thinking about making a video on this, but I’ll probably have to do a lot of research. Thanks for the idea :)
@@garbaj Thanks for your awesome videos :)
@@garbaj idk if godot has a 'springarm'(this is what unreal uses. we may have to make a makeshift one using raycasts
if you are using a "feet" collision on your controller and use this, my character flies up, so I found that you can just use the crouch height for crouching down and default height for up and it fixes this issue
NB= do not add plus or minus to the equals and only multiple crouch height by delta if not you wont move at all
...could be just me
Great video!
Thanks!
what if I want the mesh instance? I tried to do with it but the game became very laggy.
Hey how could i do sliding like that i keep momentum, and when sliding down slopes i keep getting more speed?
Problem: When you walk from flat ground onto an inclined ramp while crouching, it works fine, but when you go down the other way back onto flat ground, the player can't pass the line where the two meet. Anyone know a fix for this? Is it just me? EDIT: Seems to be only ramps maybe 45 degrees or greater.
Very nice series. Thank you.
I know there is a project out there for Godot multiplayer fonction.
So I was wondering if you ´ll show us how to make our enemy see us. (Proxy character .? ) ??
Also maybe in a future video, how our player can run on walls
I hope to make videos on those subjects in the future, I just need to learn how to do it
For some reason, when multiplying by delta, even without the clamp line written, the player just crouches a little, which is a little odd. For now, I'll just do it without multiplying by delta, but, does anyone know why this happens? My game is capped at 60 fps, but still, I would want the crouch speed to be the same for everyone. I increased the crouch speed, but the only thing that does is changing how much you crouch.
For the crouch thingy, I have an advice. Instead of changing the collision shape via code, change it via the animation player, that way you can crouch (play) and (un)crouch (play_backwards) quite seamlessly since they will be using the same animation track. PS - do not change the "scale" of the collision shape, it doesn't work, you have to change it's "shape".
I agree, in this tutorial I change the shape of the collider rather than adjust it's scale, and the result is very smooth. Thanks for the tip!
I tried this method but the animation keeps looping.
@@aryabratsahoo7474 You may have set your animation to be loopable. Check your animation track to see whether the animation is loopable.
@@Mriganka2S It's not looping in the animation player. The problem was that it was constantly being updated in the code every frame, so I fixed it by implementing various boolean checks to have it run once.
@@aryabratsahoo7474 Nice man. I would recommend you set up a nice state machine to go with the animations. They make live way easier.
I'm having a problem. I'm trying to figure what to use in place of fall.y since the controller I use is made following your improved FPS controller tutorial. I though I could use gravity_vec, but that just caused an error in my script saying "Invalid get index 'z' (on base: 'int')." Am I missing something?
When attempting to implement the headbonking to another FPS controller, it seems that when the head is bonked, my character is permanently stuck crouching. It could likely be because the way the code works is different now due to me using Godot 4.1.1 in my project vs whatever version is in this video. Has anyone else run into this, and if so, have any advice on how to fix this?
great! thanks :)
Please help me I did everything as in the video but my character does not squat
pcap.shape.height does not work in Godot 4. Do you know what is the alternative?
Best UA-camr!! Please make a tut to teleport from a translation to translation in 3D please
This one is pretty simple. Let's say you have a script on the object you want to teleport and the location you want to teleport to is Vector3(12, -32, 0). All you need to write is:
transform.origin = Vector3(12, -32, 0)
When I tried to run the program there was an error that said The function ‘move_and_slide()’ returns a value, but this value is never used
It's not an error, just a warning. You can safely ignore it
Wow that came out of nowhere
Hello, how to make power slide on fps controller?
Whenever I press the control key it doesn't work, I dunno why😐
for anyone working in third person games.... just add two collision shapes one for the legs and one for the torso, it won't influence how damage works so don't worry and then disable the torso collision shape when crouching :
if Input.is_action_pressed("crouch"):
collisionTorso.set_disabled(true)
else:
collisionTorso.set_disabled(false)
this way you can avoid a bunch of problems with clipping through terrain or if you are using free movement without fps straving the method in the video just wont work.
is there a way to do this in the improved fps code?
Hey Garbaj, I have followed this tutorial to the letter a few times over, and my character still won't crouch. The movement speed corresponds to the crouch movement speed, so it is not an input issue, and the collision box size changes also, but the axis on which it's centered stays the same, so the capsule just gets smaller on both the top and bottom edges. FYI I am using the advanced FPS controller model.
I bet it's because the advanced fps controller uses 2 collision shapes instead of one. If you want, you can get rid of the foot collision shape entirely or change the whole thing into a single cylinder shape which I believe should fix the problem, or you can code the capsule to move down when crouching if you want to stick with the 2 collision shape version
@@garbaj ah thank you!!! I ended up deleting the capsule.
Sprint with endurance/cooldown. cooldown when player walk, faster while standing, with crouch and sneak too?
I'll be covering most of this in the next advanced fps movement video
cant you just hide the mesh instance node instead of deleting it?
maybe do tutorial on vaulting?
and uh i cant crouch even with the identical code
im using your advanced controller and it worked fine with wallrunning so why doesnt this work?
if Input.is_action_pressed("crouch"):
pcap.shape.height -= crouch_speed * delta
speed = crouch_movespeed
else:
pcap.shape.height += crouch_speed * delta
pcap.shape.height = clamp(pcap.shape.height, crouch_height, default_height)
did you happen to solve that problem? 'Cause I'm having the same issue
nice
okay so i have an issue with the head bonker bit,
I am using:
if Input.is_action_just_pressed("jump") and (is_on_floor() or ground_check.is_colliding()):
gravity_vec = Vector3.UP * jump
however if i add:
if head_bonked:
fall.y = -2
it doesn't work as fall.y isn't part of the script (I am using the improved fps code).
I'm relatively new to Godot and sucky when it comes to coding, if someone could provide a solution please tell me this is doing my head in.
Its likely you already found this considering I took it from an earlier comment, but adding this instead of the head bonker code should work:
if is_on_ceiling():
>gravity_vec.y = 0
@@abstractlime101 thank you i am quite new. I added that bit of code underneath the jump code but it still hasn't worked.
Did i miss something/put it in the wrong place?
@@crowman9096 I put it above the jumping code.
It might also be if you actually put a ">", that is meant to represent an indent.
@@abstractlime101 dude thank you so damn much! What would I do without people like you
I put it above the jumping code and it works perfectly!
@@abstractlime101 one issue however haha. The last problem with the character collision clipping into the objects, causing jerky movement. I did this:
elif not is_on_ceiling():
pcap.shape.height += crouch_speed * delta
but I still face the issue. Any ideas?
There seems to be a limit on how far down the cam will go
I have an issue where the player gets stuck while moving underneath a large platform in a state between standing and crouching
I cover how to avoid this near the end of the video. Did you make sure to implement the headbonker raycast that I talked about?
@@garbaj I did. here's what I have for the code if you don't mind looking: pastebin.com/v53YZKru
Works with godot 4?
I think many dont understand how EASY it is to do somewhat complicated issues. Nice 👍
How did you make a 3D environment (like the one you made. sorry, I'm new to godot)
I suggest starting with a beginner's tutorial like this one: ua-cam.com/video/-D-IcbsdT04/v-deo.html
Garbaj Thanks!
whenever I crouch, my character falls through the ground
lol, yo thats sick, can i have those codes?
Shit Bro, Becareful what you wish for. I am redoing the tutorial for cleaner coding. And i ran into this exact problem bro. wtf, Were u trying to level everything out perfect as well?
I know this is an old comment but just in case, the player will go through the ground if the obstacle you're crouching under isn't high enough, make adjustments according to your height settings.
ah man, i really messed up. My guy is sprinting!....I gotta re do this or keep it as sprint feature
i had a - instead of a + enabled crouch, im guessing my default speed is to high making it go quicker.
The joys of programming
@@garbaj yeah its all good now, im moving on to the clipping part with raycast. Hoping by the end of the week I have a working fps controller merged with my next weeks lessons.
we need cross hair tut haha
You don't need to write subtitles, because UA-cam can generate it, maybe you should write it as info at the start. Only when you write code you should write this in bigger subtitles.
Interesting, do you find the subtitles distracting? I've been doing the subtitles myself since the autosubtitles aren't always accurate, but it's definitely a lot of work
@@garbaj I think it is unnecessary and a question of time. You don't need to subtitles, but it is better when you write gdscripts in the description, or better you use GitHub like Gdquest. You can upload files there too.
Thanks for the feedback
@Garbaj UA-cam's subtitle generation sucks when you are not speaking flawless English or when you use technical terms that aren't frequent in the dataset they use to train the neural net, I would caution against relying on youtube's machine learning translation.
i dont duck, i go slower though but my camera doesnt move down
Hey this is happening to me too, did you find a solution/fix?
@@comfyzenny no sorry
Does this still work on godot 4.1 ?
yes in principle, but his specific implementation will throw compiler errors. Ive been trying to adapt this to my modular controller and it mostly works. only problem is gravity isnt being applied correctly after the colliders been shrunk - i suspect thats more due to whatever ive done than what Garbaj did though.
Nothing happens when I try to crouch:(
everything was working fine and then after adding this my whole project is corrupted this is the second time
who do you make a gun
ua-cam.com/video/RLxDRb7yWnw/v-deo.html
@@TheWhenTheWhenItheWhen LOL
bonk
I'm using this tutorial in Godot 4
And I'm having trouble with the pcap.shape.height line, it keeps telling me, "Invaild get index "height" (on base: 'ConvexPolygonShape3D')
Does anyone know a fix for this?
if is_on_ceiling():
> gravity_vec.y = 0
im just getting errors when i play
nvm i just forgot to write something in the code, nice tutorials btw