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

КОМЕНТАРІ • 389

  • @justcnoon
    @justcnoon Рік тому +355

    deg2rad() has been renamed to deg_to_rad() in Alpha 15, if anybody's having an error.

  • @wellhellotherekyle
    @wellhellotherekyle Рік тому +140

    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!

    • @mando1570
      @mando1570 Рік тому +4

      thank you so much my guy your comment really helped me ❤

    • @gustaafmilzink
      @gustaafmilzink Рік тому +1

      Thank you!

    • @recker7017
      @recker7017 Рік тому +6

      point 1 is invalid, typehints never were necessary

    • @Micahtmusic
      @Micahtmusic Рік тому +3

      maybe not necessary, but definitely polite

    • @sechmascm
      @sechmascm Рік тому +3

      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.

  • @alechussak3750
    @alechussak3750 Рік тому +98

    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:

    • @gregoriodelimaalves6862
      @gregoriodelimaalves6862 11 місяців тому +2

      how do I do this properly

    • @koolgabieboi2816
      @koolgabieboi2816 10 місяців тому +4

      please provide a code example

    • @StephenCodess
      @StephenCodess 8 місяців тому

      thanks!

    • @NerdWithABeard
      @NerdWithABeard 7 місяців тому

      This, along with the tip for how to access the other command auto-fills, are much appreciated

    • @351c4v71
      @351c4v71 6 місяців тому

      thanks, that was very helpful

  • @erin1569
    @erin1569 Рік тому +88

    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

  • @madphoenix9826
    @madphoenix9826 7 місяців тому +3

    Always nice to find a information dense tutorial for the specific thing you wanna do ^^ Thank you very much!

  • @kevindean8312
    @kevindean8312 Рік тому +14

    I am complete noob to Godot and this tutorial was excellent. Easy to follow and loved how you actually explained everything. Thank you!

  • @DEXA_Entertainment
    @DEXA_Entertainment 7 місяців тому +2

    Very on point, short and helpful! Thank you for teaching not only me but a lot of people too. 😊👍

  • @theaussieninja8176
    @theaussieninja8176 9 місяців тому +1

    thank your Bramwell for these tutorials I love how you go into great depths on everything I wish more tutorials went into such depth

  • @sweetdog2398
    @sweetdog2398 4 місяці тому +5

    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.

  • @aullvrch
    @aullvrch Рік тому

    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!!

  • @petethorne5094
    @petethorne5094 Рік тому +13

    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!

  • @JohnFenlon
    @JohnFenlon Рік тому

    Just started using Godot, very helpful video to get up & running quickly, many thanks 👍

  • @PAPRPL8
    @PAPRPL8 2 роки тому

    Thanks for the tutorial; found it quite helpful and moved at a pace I found keep up with as a beginner.

  • @chrisfritz7545
    @chrisfritz7545 Рік тому +5

    Just wanted to say thanks for sharing your knowledge. Keep making these videos. You have a great way of explaining what you are doing.

  • @riisezz0
    @riisezz0 Рік тому +4

    Hey, man. I'm new to Godot and you've really been helping in the transition to Godot 4. I really appreciate you!

  • @harryudal3115
    @harryudal3115 6 місяців тому

    Thank you so much for all the work you have done, this has given me so much help. And I thank you

  • @swedishgamedev
    @swedishgamedev Рік тому

    Thank you for this very clear tutorial! It helped a bunch!

  • @wukerplank
    @wukerplank Рік тому

    Very interesting, thank you for putting this together!

  • @cyphfrix1620
    @cyphfrix1620 Рік тому +1

    thanks so much bro, your tutorial is very easy to follow and quick

  • @bluecheese1066
    @bluecheese1066 Рік тому

    Very nice intro to some 3D player basics. Thank you very much!

  • @pisel5945
    @pisel5945 Рік тому

    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

  • @ViralShorts8303
    @ViralShorts8303 Рік тому

    Hey man thank you so much its working very good and i am very happy!! you just got a Subscriber😉

  • @chrismelton6935
    @chrismelton6935 8 місяців тому

    Thank you! Explained well and straight to the point.

  • @VGV0
    @VGV0 4 місяці тому

    thanks, great video! You explained every step. Much appreciated!

  • @sogimarvaz
    @sogimarvaz 10 місяців тому

    Very nice video, thank you so much Bramwell.

  • @PanIsTrying
    @PanIsTrying 5 місяців тому +1

    Thank you kindly for your help in this tutorial. It made me a subscriber

  • @pommefrites
    @pommefrites Рік тому +1

    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!

  • @Phryj
    @Phryj 6 місяців тому

    This was a big help, thank you!

  • @piimae
    @piimae Рік тому +1

    Exceptionally well done tutorial, straight to the point and just the right amount of explaining of the details of the mechanics. Thank you!

  • @I_can-t_GAMING
    @I_can-t_GAMING 11 місяців тому +2

    والله انا كنت واقف على ان الشخصية بتخترق الارض لكن دا اكتر فيديو فادني فشكرا جدا
    💖💖💖💖❤❤
    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

  • @dairygecko
    @dairygecko 5 місяців тому

    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)

  • @acedefective2220
    @acedefective2220 5 місяців тому

    thank you, this tutorial was very accessible and helpful

  • @apersimmon
    @apersimmon 6 місяців тому

    I applied this to a 3rd person player but it was very useful, thanks!!

  • @sethwhite4635
    @sethwhite4635 2 роки тому +1

    Awesome, thanks for the video!

  • @user-nl6jx4oz7n
    @user-nl6jx4oz7n 10 місяців тому

    Thank you bro. I am a boy who need these types of tutorials. LOVE FROM PUNJAB

  • @BuffHobbit
    @BuffHobbit Рік тому

    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

  • @wolcamophone4783
    @wolcamophone4783 2 роки тому +2

    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.

  • @KyleRassweiler
    @KyleRassweiler Рік тому +1

    Is it possible to have the CharacterBody3D interact with physics objects?

  • @ShadowWolf936
    @ShadowWolf936 6 місяців тому

    some of the camera script didn't pop up for me like it did for you. anyway i can get the mouse input part?

  • @TheInfiniteVoid
    @TheInfiniteVoid Рік тому

    what is the arrow in scrip after inputevent and how do i put it there?

  • @meteroy17
    @meteroy17 Рік тому +1

    Is there a way to add other functions like interact, running, crouching, and all that?

  • @toxslix
    @toxslix Місяць тому

    Thanks alot very useful video much love to ya mate

  • @seeranos
    @seeranos Рік тому

    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?

  • @RivenbladeS
    @RivenbladeS 3 місяці тому

    why doesnt godot have buttons to instantiate cube plane culinder etc like unity and you have to set the mesh instance yourself?

  • @Clan_GG
    @Clan_GG Рік тому

    How would I change the mouse for looking to touch screen I currently have
    If event is InputEventscreendrag:
    ≤. ≥ input.set

  • @Drachenbauer
    @Drachenbauer Рік тому +5

    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.

  • @devinpeck6664
    @devinpeck6664 11 місяців тому

    Love this! Thank you so much.

  • @samuellangford6126
    @samuellangford6126 10 місяців тому

    something is wrong with my camera.rotate_x(-event.relative.y * 0.01) does it need any nodes?

  • @odrzuconyromantyk
    @odrzuconyromantyk Рік тому

    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.

  • @RedCatGD
    @RedCatGD 8 місяців тому

    Thanks a lot ! Helped me out a tone ❤

  • @mr.e4327
    @mr.e4327 4 місяці тому

    Great tutorial, thanks mate!

  • @pacobrian7547
    @pacobrian7547 Рік тому

    you are a genius, you solved my problem of collisions

  • @Will_-it3mh
    @Will_-it3mh 7 місяців тому

    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?

  • @Mineblox-33333
    @Mineblox-33333 6 місяців тому

    Where do I put the deg_to_rad() at so then there will be no error?

  • @idk-gq8tw
    @idk-gq8tw 7 місяців тому +1

    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").

  • @Briedisons69
    @Briedisons69 11 місяців тому

    get_mouse_mode is not showing up for me is there a new renamed version of it?

  • @RealJesus
    @RealJesus 10 місяців тому +1

    help everytime i look down/right the poc starts spinning and it's uncontrollable

  • @mikhaylastovall1770
    @mikhaylastovall1770 4 місяці тому

    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)

  • @trueblue97
    @trueblue97 9 місяців тому

    This is all cool and good, but how do I make camera controls with the right stick on a controller instead of the mouse?

  • @Rebel_MC2355
    @Rebel_MC2355 Рік тому

    at 12:21 how do you get those lines of code? im typing them in rn and its very annoying

  • @ois718
    @ois718 9 місяців тому

    for func input it doesnt say inputevent void? im on version 4.1

  • @bleh1186
    @bleh1186 Рік тому

    Some extreme jitteryness while looking and moving, any idea what's going on with this?

  • @18_leafclover
    @18_leafclover Місяць тому

    I'm getting an error on lines 20, 21 Identifier camera not declared in the current scope

  • @Creeg81
    @Creeg81 4 місяці тому

    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

  • @petermuster5734
    @petermuster5734 4 місяці тому

    Whenever I run it, my camera isnt able to look down somehow and it offsets itself up by a lot

  • @kirb9166
    @kirb9166 9 місяців тому +2

    12:22 Where did he get this string of code from i dont understand how he got it

  • @adaml.5355
    @adaml.5355 2 роки тому

    This is an extremely helpful video.

  • @burntbrownie
    @burntbrownie 5 місяців тому

    Anyone know why the playermodel gets frozen sometimes when walking into walls?

  • @shigsy2630
    @shigsy2630 Рік тому +2

    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.

    • @mrunion
      @mrunion Рік тому

      Care to share your experiment with C#?

  • @itokuun
    @itokuun Рік тому

    Awesome video man

  • @PlasmaGhost303
    @PlasmaGhost303 2 місяці тому

    the tutorial was great! thank you

  • @explainmydrawing8986
    @explainmydrawing8986 Місяць тому

    Where should I add the jump animation

  • @Mrtubb158
    @Mrtubb158 9 місяців тому

    How do you get the star icon thing at 13:23

  • @YOZA.
    @YOZA. 8 місяців тому +3

    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

  • @olmrgreen1904
    @olmrgreen1904 Рік тому

    Awesome video!

  • @felixflache1437
    @felixflache1437 Рік тому

    My up and down camera movement doesn't work. But I have exactly the same code: What can I do?

  • @NuggetEater-ws7yr
    @NuggetEater-ws7yr Рік тому

    for some reason when i tried to get a character body it didnt show up when i searched it

  • @MP-pv4eb
    @MP-pv4eb Рік тому

    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.

  • @Revenant_Jay09
    @Revenant_Jay09 Рік тому

    How do you fix a null instance?

  • @zoolanderbestmovie2
    @zoolanderbestmovie2 Рік тому

    cool good job easy to learn continue the hard work

  • @NarekAvetisyan
    @NarekAvetisyan Рік тому +2

    Very nice!
    Can you also make a simple tutorial like this for a Age of Empires style camera?

  • @hayleypeterson7007
    @hayleypeterson7007 5 місяців тому

    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:

  • @S.Johannesson
    @S.Johannesson 5 місяців тому

    Thank you for this!

  • @slightlyangrybeluga
    @slightlyangrybeluga Рік тому

    Whenever i try to play it, it crashes, any reason why?

  • @MidvightMirage
    @MidvightMirage 4 місяці тому

    how do i change the sensitivity?

  • @I9096I
    @I9096I 9 місяців тому

    How Did You Get The Arrow In Godot?

  • @badmusicproducer_offical
    @badmusicproducer_offical 24 дні тому

    HOW DO I MAKE THE CAMERA EVEN LESS FAST?

  • @bblucat_
    @bblucat_ Рік тому

    Great Video keep it up!

  • @IMDAGER
    @IMDAGER Рік тому

    I learned so much thank you

  • @cultofape
    @cultofape 2 роки тому +12

    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

    • @BramwellWilliams
      @BramwellWilliams  2 роки тому +7

      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

    • @grarldbrown6812
      @grarldbrown6812 10 місяців тому

      I'm just hoping to use LMB method for camera3D, similar as the way of tutorials does but classic rpg.

  • @delegalco
    @delegalco Рік тому

    How he get the arrow on the some lines of code ->

  • @pedrofonseca9062
    @pedrofonseca9062 Рік тому

    How i do the player run???

  • @hectora.3220
    @hectora.3220 4 місяці тому

    This is great. Short and usefull.

  • @eyejar234
    @eyejar234 Рік тому

    Thank you very very much with your help I finaly make 3d first person controller 😃😃😃

  • @Aneemkhann
    @Aneemkhann Рік тому

    i did what you told me but my character body is not moving nor the camera

  • @darkBuster2002
    @darkBuster2002 Рік тому +3

    there is a problem for me Line 17:Assignment is not allowed inside an expression.Line 18:Assignment is not allowed inside an expression.

    • @foldysnootmack
      @foldysnootmack Рік тому +1

      I’m having the same issue. Have you figured out how to fix it?

    • @nezbro2011
      @nezbro2011 Рік тому +3

      @@foldysnootmack Double equals so type ==

    • @user-ug6hh4qg3n
      @user-ug6hh4qg3n Рік тому +3

      @@nezbro2011 Bloody hell really??? DOUBLE EQUALS??? God damn. Good thing I check the comments for answers... Fucking double equals...

    • @nezbro2011
      @nezbro2011 Рік тому +1

      @@user-ug6hh4qg3n Yeah not really obvious at first considering there isn't a gap in between the two.

    • @darkBuster2002
      @darkBuster2002 Рік тому

      @@foldysnootmack No

  • @Ciac18
    @Ciac18 Рік тому

    thank you so much for this tutorial

  • @JorgeRosa
    @JorgeRosa 2 роки тому +2

    Very cool!

  • @ThaMentalGod2003
    @ThaMentalGod2003 Рік тому

    yo thx for making this godot 4 tutorial 😎

  • @qcs434
    @qcs434 Місяць тому

    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