Tutorial: First Person Movement In Godot 4
Вставка
- Опубліковано 21 лип 2024
- Godot 4 Is all new and exciting, so here's a beginners guide to get up and running with a first person controller in only a couple lines of code!
🔗 Code link: gist.github.com/bramreth/2a62...
📰 Check out my Godot 4 course here! bramwell.itch.io/godot-4-begi...
✨ Socials ✨
🐦 Twitter: / bramreth
💬 Discord: / discord
☕ Buy me a coffee: www.buymeacoffee.com/bram
🎮Games: bramwell.itch.io/
🕺TikTok: / bramreth
🤖 The ninth alpha release for Godot 4 I used in the video is here:
downloads.tuxfamily.org/godot...
Chapters:
00:00 Making the floor
01:41 Creating the player body
03:35 Adding a first person Camera
05:20 Adding a WorldEnvironment
06:47 Making the player move
07:50 Using custom inputs
09:40 Creating the camera script
16:05 Orienting motion to the camera direction
17:36 Other Godot 4 educational material
#Godot4 #GodotEngine #Gamedev
deg2rad() has been renamed to deg_to_rad() in Alpha 15, if anybody's having an error.
thank you!
thanks!!
You're a life saver!
Good. That 2 was cringey as hell :D
@@NirielWinx no it wasnt u mf I will fight u
So two things had changed since this video was published...
1. The _unhandled_input part, the code should read like this now: _unhandled_input(event):
(All the stuff after on that line in Branwell's code is no longer necessary)
2. deg2rad() has been renamed to deg_to_rad()
(Thank you to Andy Reed for pointing this one out)
Thanks for the wonderful tutorial Bramwell!
thank you so much my guy your comment really helped me ❤
Thank you!
point 1 is invalid, typehints never were necessary
maybe not necessary, but definitely polite
The other stuff on that line improves performance by a huge margin. The engine doesn't have to guess what everyting is supposed to be and it runs those operations faster. If you want a good game, it is necessary.
set_mouse_mode() and get_mouse_mode() have been removed since this video was made. You can now get/set the Input.mouse_mode property directly:
SET: Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
GET: if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
how do I do this properly
please provide a code example
thanks!
This, along with the tip for how to access the other command auto-fills, are much appreciated
thanks, that was very helpful
Btw, if you want to have the same autocomplete as Bramwell at 11:47, you should go to Editor>Editor Settings>Text Editor>Completion>Add type hints
TYSMMMMMMM
Thanks
what a legend
Thank you.
Bro thanks alot I was scared that's I did wrong
Always nice to find a information dense tutorial for the specific thing you wanna do ^^ Thank you very much!
I am complete noob to Godot and this tutorial was excellent. Easy to follow and loved how you actually explained everything. Thank you!
same
Very on point, short and helpful! Thank you for teaching not only me but a lot of people too. 😊👍
thank your Bramwell for these tutorials I love how you go into great depths on everything I wish more tutorials went into such depth
A cool thing you can do with this part:
var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_backwards")
var direction = (neck.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
if is_on_floor():
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
if is_on_floor():
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
is right before the velocity stuff you can add a if is_on_floor so that you cant change directions midair, or you can use an else comman so that you can but its less.
this made me subscribe! By far the most useful and educational vid on this topic I I have seen thus far, with the added bonus of being in godot 4!!
This is absolutely brilliant! I just downloaded the Beta (woo!) and within a few mins I had a player running around my test scene. Bought your course a while back and now that things are getting more stable, I'm going to have to stop avoiding building a game!
So was Godot 3.x not very stable?
Just started using Godot, very helpful video to get up & running quickly, many thanks 👍
Thanks for the tutorial; found it quite helpful and moved at a pace I found keep up with as a beginner.
Just wanted to say thanks for sharing your knowledge. Keep making these videos. You have a great way of explaining what you are doing.
Hey, man. I'm new to Godot and you've really been helping in the transition to Godot 4. I really appreciate you!
Thank you so much for all the work you have done, this has given me so much help. And I thank you
Thank you for this very clear tutorial! It helped a bunch!
Very interesting, thank you for putting this together!
thanks so much bro, your tutorial is very easy to follow and quick
Very nice intro to some 3D player basics. Thank you very much!
thank you i had no idea how to do something in godot because there was always errors, i didnt think that i will succed following the first tutorial that i clicked on
Hey man thank you so much its working very good and i am very happy!! you just got a Subscriber😉
Thank you! Explained well and straight to the point.
thanks, great video! You explained every step. Much appreciated!
Very nice video, thank you so much Bramwell.
Thank you kindly for your help in this tutorial. It made me a subscriber
same
Hey, kind of semi-related but this video helped me fix a long-standing issue with the stair catcher in my character controller being offset if my character starts the level rotated at all, which is fine if the player always starts in the same direction with 0,0,0 rotation, but not great if they do, which is most often the case. It has to do with the lines you highlighted around 17:17 and I used a very similar block to calculate the position of the stair catcher ray cast to swing around in the direction of player movement. I then just had to offset it vertically from the neck height to be near the floor and now it works perfectly no matter which direction the player starts in. Anyway, it had been bugging the hell out of me for a whiiiiile, so thanks for your help!
This was a big help, thank you!
Exceptionally well done tutorial, straight to the point and just the right amount of explaining of the details of the mechanics. Thank you!
والله انا كنت واقف على ان الشخصية بتخترق الارض لكن دا اكتر فيديو فادني فشكرا جدا
💖💖💖💖❤❤
By God, I was standing on the fact that the character penetrates the ground, but this is the most useful video, so thank you very much
thank you so much, i was watching another tutorial and it skipped the part in which the player was created, im a complete newbie, now i can continue the other tutorial (which was about creating a map)
thank you, this tutorial was very accessible and helpful
I applied this to a 3rd person player but it was very useful, thanks!!
Awesome, thanks for the video!
Thank you bro. I am a boy who need these types of tutorials. LOVE FROM PUNJAB
16:00 this is all good but does the player body rotate with the neck? so that the head doesn't just spin around while the body is not. So the body is facing the same way we are looking
I wish there was a video that really explained how to better understand movement functions tied to kinematic bodies so that you could know exactly what kind of bhopping or air strafing glitches to put in movement fps controllers.
Is it possible to have the CharacterBody3D interact with physics objects?
some of the camera script didn't pop up for me like it did for you. anyway i can get the mouse input part?
what is the arrow in scrip after inputevent and how do i put it there?
Is there a way to add other functions like interact, running, crouching, and all that?
Thanks alot very useful video much love to ya mate
Glad it helped!
Why do we rotate the neck node for horizontal movement, but the camera itself for vertical movement? Is it because we want the rotation clamped for the camera?
why doesnt godot have buttons to instantiate cube plane culinder etc like unity and you have to set the mesh instance yourself?
How would I change the mouse for looking to touch screen I currently have
If event is InputEventscreendrag:
≤. ≥ input.set
i didn´t add a neck node, i rotate the whole character around y instead with the mouse, so i just didn´t get the problem of fixed forward direction, that´s described at 16:15.
It feels just like a fully functional first person charecter.
Love this! Thank you so much.
something is wrong with my camera.rotate_x(-event.relative.y * 0.01) does it need any nodes?
Thank you! This one works and is really easy to follow. One question though, how can i make camera clamp on y axis. Tried to replicate code from x axis but nothing works.
Thanks a lot ! Helped me out a tone ❤
Great tutorial, thanks mate!
you are a genius, you solved my problem of collisions
if i tlit the camera to the up and down limits it goes haywire and starts spining to the sides. how do i fix that?
Where do I put the deg_to_rad() at so then there will be no error?
Can someone help, i started the scene and when i try to rotate is freezes and it says kennedy.gd 10 @ _ready(): node not found: "neck/camera3d" (relative to "/root/node3d/characterbody3d").
get_mouse_mode is not showing up for me is there a new renamed version of it?
help everytime i look down/right the poc starts spinning and it's uncontrollable
How would I slow down how fast the mouse is moving in game? It's a little to jerky for my preference. (also pls explain it to me like I'm an idiot please and thank you)
This is all cool and good, but how do I make camera controls with the right stick on a controller instead of the mouse?
at 12:21 how do you get those lines of code? im typing them in rn and its very annoying
for func input it doesnt say inputevent void? im on version 4.1
Some extreme jitteryness while looking and moving, any idea what's going on with this?
I'm getting an error on lines 20, 21 Identifier camera not declared in the current scope
i have a problem where when i look up or down, the camera starts spinning around and gets messed up. anyone kjnow how to fix this
Whenever I run it, my camera isnt able to look down somehow and it offsets itself up by a lot
12:22 Where did he get this string of code from i dont understand how he got it
This is an extremely helpful video.
Anyone know why the playermodel gets frozen sometimes when walking into walls?
I've written the equivalent in C#, which requires a few differences other than the syntax (e.g. you cant directly modify the Rotation properties so need to create a separate Vector3 and set it to the Rotation, then manipulate and clamp that new Vector 3 before setting the Rotation back to the Vector3). This generally works fine but there is one odd behavior... I can't move the mouse and walk at the same time. So I can stand still looking around with the mouse, or I can walk around with the keys, but I can't do both at once. Any ideas? I suspect differences in how C# handles the Input events. If I'm holding W to walk and then move the mouse at the same time, it doesn't appear to be passing an InputEventMouseMotion event to my _UnhandledInput method. It executes the code in that method but not within the "if" that's checking for the InputEventMouseMotion, so presumably the event is something else (like the key im holding down to walk). If I stop moving (let go of W) and move the mouse, it correctly passes the InputEventMouseMotion to the _UnhandledInput method and I can look around fine.
Care to share your experiment with C#?
Awesome video man
the tutorial was great! thank you
Where should I add the jump animation
How do you get the star icon thing at 13:23
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
# Get the gravity from the project settings to be synced with RigidDynamicBody nodes.
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
@onready var neck := $Neck
@onready var camera := $Neck/Camera3d
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
elif event.is_action_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
if event is InputEventMouseMotion:
neck.rotate_y(-event.relative.x * 0.01)
camera.rotate_x(-event.relative.y * 0.01)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-30), deg_to_rad(60))
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Handle Jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("left", "right", "forward", "back")
var direction = (neck.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_t
Awesome video!
My up and down camera movement doesn't work. But I have exactly the same code: What can I do?
for some reason when i tried to get a character body it didnt show up when i searched it
thanks for this tutorial!
I found for me the camera was very twitchy. I'm not sure if there are better solutions for this, but this is what I ended up doing:
var x_mov = event.relative.x / 1.7
var y_mov = event.relative.y / 1.7
neck.rotate_y(-x_mov * 0.01)
camera.rotate_x(-y_mov * 0.01)
you might be able to do it in-line (so, neck.rotate_y((event.relative.x / 1.7) * 0.01) for example), but the variables help my readability. Also, change 1.7 to whatever value is comfortable. Larger value will be slower, "smoother" motion.
How do you fix a null instance?
cool good job easy to learn continue the hard work
Very nice!
Can you also make a simple tutorial like this for a Age of Empires style camera?
This is still such a great tutorial, but I wanted to add that if anyone's gotten a bug when adding camera movement, the fix for me was this line to check MouseMotion was the event type:
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and event is InputEventMouseMotion:
Thank you for this!
Whenever i try to play it, it crashes, any reason why?
how do i change the sensitivity?
How Did You Get The Arrow In Godot?
HOW DO I MAKE THE CAMERA EVEN LESS FAST?
Great Video keep it up!
I learned so much thank you
Love your tutorials! If I can make a request - 3rd person 3d with LMB click on ground movement and proper player rotation. I just can´t make it 100%right
I could do something like that for sure ^^ I've done something like that with gridmaps for tiles you can move to: twitter.com/bramreth/status/1453508866134663174
I'm just hoping to use LMB method for camera3D, similar as the way of tutorials does but classic rpg.
How he get the arrow on the some lines of code ->
How i do the player run???
This is great. Short and usefull.
Thank you very very much with your help I finaly make 3d first person controller 😃😃😃
i did what you told me but my character body is not moving nor the camera
there is a problem for me Line 17:Assignment is not allowed inside an expression.Line 18:Assignment is not allowed inside an expression.
I’m having the same issue. Have you figured out how to fix it?
@@foldysnootmack Double equals so type ==
@@nezbro2011 Bloody hell really??? DOUBLE EQUALS??? God damn. Good thing I check the comments for answers... Fucking double equals...
@@user-ug6hh4qg3n Yeah not really obvious at first considering there isn't a gap in between the two.
@@foldysnootmack No
thank you so much for this tutorial
Very cool!
yo thx for making this godot 4 tutorial 😎
Brother i have. Big problem
My charcter run at out of surface and i want my chacter run at surface plese reply i have a big problem