Godot Recipe: 3D KinematicBody: Align with terrain

Поділитися
Вставка
  • Опубліковано 7 лют 2025
  • Learn how to align a 3D object with a surface, such as terrain or platforms.
    Text version & link to download tank model:
    godotrecipes.co...
    Support me on Patreon: / kidscancode

КОМЕНТАРІ • 109

  • @zaidkiwan5168
    @zaidkiwan5168 4 роки тому +4

    BEST godot tutorial EVER! this should be the best godot channel and everyone should know!

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

    Omg thank you, finally someone explain clearly and properly how to align object with surface! Amazing!

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

      did this tutorial work on godot 4?

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

      @@Ayteo. Math is the same, you just use move_and_slide() and floor_stop_on_slope=true instead of move_and_slide_and_snap().

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

      @@TheLastPhoen1x thank you

  • @necaton
    @necaton 4 роки тому +5

    please upload some more godot tutorials. they are the best on the whole internet!

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

    this is taught so well, one of the best tutorials on youtube

  • @thefamousrat8852
    @thefamousrat8852 4 роки тому +1

    Super video ! Solved a similar problem I had within minutes. Thanks !

  • @MrBoczald
    @MrBoczald 4 роки тому +2

    I love your videos. I started learning with them. Before that, it was hard for me to start anything. Now I have my way, thanks!

  • @BrianRogers3D
    @BrianRogers3D 4 роки тому +12

    Well done! You're a very good teacher

  • @cmds.learning7426
    @cmds.learning7426 4 роки тому +2

    Great video, thank you!

  • @mrnalaiq3196
    @mrnalaiq3196 4 роки тому +1

    I love your videos

  • @mitchellstephens4228
    @mitchellstephens4228 4 роки тому +2

    please keep doing more vids

  • @hirochima26
    @hirochima26 4 роки тому +5

    well i guess you love tanks ( thnx for you tutorials

  • @rorballfrias1750
    @rorballfrias1750 4 роки тому +2

    More videoss plisss
    ...................
    SO much waiting
    ....................

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

    If anyone following through this tutorial experiences a weird grey screen after calling on the raycast, followed by the det == 0 error in the debugger, you need to add this if statemet before using a method on the raycast. It seems this can solve most problems related to raycasts is to just first check whether or not they're actually colliding before performing some function with them.
    if $RayCast.is_colliding():

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

      My vehicle still doesn't behave correctly, the axis it aligns to is wrong, it passes through surfaces sometimes, jitters etc.

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

      @@npc_blob1609 this is the next problem I'm trying to solve. If you're experincing the same issue I am, it's because your player model is a large rectangle or otherwise covers an area much larger than what that single raycast could reliably cover. In my case my character is a slug; basically an elongated shape with a single raycast in the center. Because of this, the nose of the model can ride up a steep slope lifting the raycast off the ground before it has a chance to collide with the ramp, causing all sorts of weird collisions. My solution to this is to add more rays, and organize them in a folder. You can do this by creating a basic node and organizing all your characters rays as children of that node. Let's say you call the node "rayfolder", you'd wanna create a for loop that iterates over all the rays in rayfolder, then orients the player to an average of all rays for each frame.
      Something like
      for ray in $rayfolder:
      I set this project down for a minute so I haven't come up with the exact solution yet, but I'm getting my ideas from the "how to climb any wall" tutorial by NAD LABS here on UA-cam, check that out and ponder over it, and a light bulb may come on over your head
      Good luck! :)

  • @J_A_C023
    @J_A_C023 11 місяців тому +1

    Its not quite accurate on a sphere, from what I did. Along the 'equator' of the sphere, my test character actually starts to rotate a bit to the side and then the further I go away from the 'equator', it begins to rotate back. Is this due to some global y somewhere, where it become 0 at that point or something?

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

      I came up with a better way (for my script at least). First get the vectors of the normal by reverse calculating the cross products and put them in an array and return that to the physics_process, then in the physics_process, call it with a variable, something called 'vectors' or whatever. Then use that array [0, 1, 2] as x, y, z (or other order needed since its a basis transform), and then say rotation_basis = Basis(vectors[2], vectors[1], vectors[0]) and ortho that to keep the scale. Then, you can make a new Transform and use it's method 'looking_at' since I don't think kinematicbody 3d has any look methods, might just be rigid idk but I dont think it does. And within that Transform make it look at vectors[2], vectors[1], because those are the x, and y cross products like how you used them to fix the orientations, and you can then simply set the global_transform.basis to = rotation_basis. Hopefully that makes sense lol.

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

      Nvm your way is better :P

  • @fernanboxfrias381
    @fernanboxfrias381 4 роки тому +2

    How did you change the color of the letter of the script

  • @proatgaimz
    @proatgaimz 4 роки тому +1

    i am having trouble rotating on my local y axis while this function is running. or the rotating works but the alignment with the terrain gets paused while i am rotating.

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

    you are a life saver!! :')

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

    tried almost the same method but it seems to always shutter between 2 positions if you stand in any convex edge, is really consistent to have this issue and idk what remotely is happening, is impossible for a raycast to be so consistenly stepping on the perfect edge so it cant be that, tried to draw the movement and angles to see if its somewhat able to return to the previous edge but it doesnt seem to be able to? i can see in this video the issue seems to also be present

  • @OGTV1up
    @OGTV1up 4 роки тому +1

    also love your vids chris

  • @viniguerrero
    @viniguerrero 4 роки тому +2

    Nice video Chris! If I could give a suggestion for another tutorial, I'd say how do you identify if a KinematicBody is Jumping or Falling when is_on_floor is false, curious to see your approach on cases like this. Your videos help me a lot! Thank you for all the great content, keep it up

    • @codingkam1252
      @codingkam1252 4 роки тому +4

      Maybe :
      if !is_on_floor:
      if velocity.y < 0:
      jump()
      elif velocity.y

    • @viniguerrero
      @viniguerrero 4 роки тому +2

      @@codingkam1252 does that assume ground.y starts in 0 or could use like have a platform in y-20 where that is considered as the ground?

    • @bitmammothOG
      @bitmammothOG 4 роки тому +1

      @@viniguerrero Since its only affecting velocity it should be fine.

  • @tomaszgoral8154
    @tomaszgoral8154 3 роки тому +1

    Would I be able to get the angle of the normal by getting the dot product from the up vector 3 and the ground normal?

  • @ara7546
    @ara7546 4 роки тому +3

    i get this error
    ERROR: Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quat() or call orthonormalized() instead.

    • @root-kg6uz
      @root-kg6uz 4 роки тому +1

      I have the same problem and same error, and then the camera sets on the tank and can only go forward
      I am using linux and have a compiled version of godot with zylann but also happens in stable 3.2.1

    • @BelfortDev
      @BelfortDev 3 роки тому +1

      I have the same problem and same error too

  • @nabilabedkarim7112
    @nabilabedkarim7112 4 роки тому +1

    HELLO
    i got a problem. i wich i can get your help
    when i try to open godot this masseg show up :
    your video card driver does not support any of the supported opengl versions
    and i cant update my windows 7 because is pirated
    can you help me pls

  • @douglasmatos7652
    @douglasmatos7652 4 роки тому +1

    This didn't work for me....I need to align a hoverbike in concave tubes....anyone please help me...I dont understand how to manipulate raycasts in godot...I'm very frustrated at really because the visuals are ok...the most mechanics is ok...but....the terrain alignment..not ok.....

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

    So, this tutorial has mostly worked perfectly, right up until the part where you added the RayCast and changed the code to reflect that.
    For some ungodly reason, despite my code being entirely identical to yours, when I change the code to reflect what you do at 12:20, when I run the game, the screen is entirely grey. When I change the camera to be stationary and not parented to the player, I just... don't see the player. And the debugger keeps saying "Invert: Condition "det == 0" is true". I don't know what any of this means, and why exactly doing the exact same thing you did is causing the code to completely break despite it working exactly the same just before that.

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

      SAAAME! I've been seriously scratching my head about this, I believe it could be related to updates with the engine changing the functionality of the code. I've realized the interpolated camera utilized in the last tutorial has been deprecated and removed for godot 4 and apparently no longer works in versions of godot 3. Thankfully he's included what appears to be a fix for it, on the "interpolated camera" page on the KidsCanCode website, although I haven't gotten that working yet but I'm grateful there is a fix. What this does show us though is that as the Godot landscape changes old methods and ways of doing things may break and have to be re-engineered.
      I'm going to try and find a fix for this, I've been using this video as an invaluable resource and I'm very happy that KidsCanCode has taken the time to share this knowledge. I feel like the fix could be something simple and have to do with the way the nodes $raycast is being accessed that somehow breaks the whole thing.
      I'm going to mess around with the Climbing System developed by Nad Labs on youtube, as it does something very similar. I'll reach out to the Godot Community on discord as well to see if anyone is familiar with this, because I am in great need of finding a solution.
      I'll let you know if I come up with something, I'm running into that same error:
      Error: Invert: Condition "det == 0" is true.
      C++ Source: core/math/basis.cpp:69 @ Invert()

    • @MagmaSloth64
      @MagmaSloth64 2 роки тому +3

      UPDATE: I'll update this with my findings. I saw in the video he's using the stable Godot 3.2.1 release while I was using the mono build of 3.2.3, and although I've tested this scene on 3.5 and on 3.2.1 to see if maybe it only worked on the slightly older version, I've ruled this out as a potential cause since the problem still occurs. This leads me to believe there is some sort of dependency in his setup possibly reliant on a previous tutorial that was not mentioned in this video.
      SOLUTION:
      Found the solution, simply add this before calling the .get_collision_normal() method on the $RayCast:
      if $RayCast.is_colliding():
      So it'd look something like this:
      if $Rayzor3.is_colliding():
      var normy = $Rayzor3.get_collision_normal()
      var xform = align_with_y(global_transform, normy)
      global_transform = global_transform.interpolate_with(xform, 0.2)

  • @mina7572
    @mina7572 4 роки тому +1

    Can you explain Gravity Point for Area2D in Godot?
    I made an Area2D as a child of a RigidBody2D and set all the gravity point related settings on, but it doesn't attract any instances of itself when they get within each others Area2D collisionshape.
    Is there an alternative way to make a RigidBody2D have gravity or to be sticky or magnetic with instances of itself?

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

    there is a problem when I converted this to C# the formula works but just bearly, this is the function code
    private Transform _bruh(Transform Xform, Vector3 normal)
    {
    Xform.basis.y = normal;
    Xform.basis.x = -Xform.basis.z.Cross(normal);
    Xform.basis = Xform.basis.Orthonormalized();
    return Xform;
    }
    the kinamtic body rotates to the surface normal but it does not rotate to it correctly leaving the body rotated just alittle bit off to the side. making the body slide around the floor like it's ice or somthing. Please help me with this I don't know why it's doing this.

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

    stupid question, but could you not do this with the built in function using a vector3.left instead of vector3.up?....e.g. look_at(collision.point,Vector3.LEFT)??? I have not tried this..I'm simply here because I want to research this before I blindly do something stupid that will cause me to do a bunch of vector subtraction and then setting rotations semi manually. Love your site and channel BTW. thumbs up given.

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

    Could you provide an example project file to compare to in future? This straight up doesn't work in the current version, seizure-inducing camera snapping to new angles, quaternion errors, rotating to face the wrong axis on inclines, etc at different stages of the video, even when copy-pasted straight from the KidsCanCode page.

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

      You may have something wrong with your models and/or your collision setup - it's not just the code. I opened the old project in 3.5 and it works perfectly fine. Here is the project - there are several example scenes in there, see the "3DExample" folder for this video's content. github.com/kidscancode/shooting_demos

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

      @@Kidscancode Thanks for the reply. You're right, the project works in the current version. I'm really confused then - I've made sure the transforms in my vehicle scene are at 0 facing negative Z, tried many different collisionShapes keeping them simple and trying to match what's in the tutorial, different raycast lengths etc, but it still messes up.

  • @merterisen
    @merterisen 4 роки тому +1

    I should learn Python for my job but also I want to make a game 2D to phone hobby. What you suggest me to learn unity or godot or pygame?

  • @ara7546
    @ara7546 4 роки тому +1

    can you do same tutorial but with character ? (kinematic character, rigid)

  • @cmds.learning7426
    @cmds.learning7426 4 роки тому

    hi MR KidsCanCode 。。。waiting you for a long time..where are you ?

  • @barellevy6030
    @barellevy6030 4 роки тому +1

    Great video, thank you!
    Was wondering, can I take the average normal of all the edges we are colliding with?

    • @Kidscancode
      @Kidscancode  4 роки тому +1

      Sure - that's essentially what the raycast is doing.

  • @thezadek
    @thezadek 4 роки тому +2

    doesn't this allow the tank to climb the walls?

    • @Kidscancode
      @Kidscancode  4 роки тому +1

      The downward facing raycast isn't going to hit a vertical wall. But you can also check the angle of the normal before you rotate - use the dot product and if it's above a certain value, don't align.

  • @jumageorge5405
    @jumageorge5405 4 роки тому +1

    Hello! Can godot make a steering wheel control for android driving game

  • @fernanboxfrias381
    @fernanboxfrias381 4 роки тому +2

    Upload please

  • @santoshgujar5237
    @santoshgujar5237 4 роки тому +1

    Sir Please can you add a Tutorial 2d racing game where vehicle move left right and background move left right, like old days, Please its a Request

  • @codingkam1252
    @codingkam1252 4 роки тому +1

    the stop on slope function doesnt work in the newest version of godot, how do you stop sliding on a slope

    • @Kidscancode
      @Kidscancode  4 роки тому

      See the previous video. `move_and_slide_with_snap()` works well.

    • @codingkam1252
      @codingkam1252 4 роки тому

      @@Kidscancode I found a video with a very simple fix heres the link:
      ua-cam.com/video/rgFRKGGqy6w/v-deo.html
      it worked perfectly fine for me

  • @jamesxxxyz8775
    @jamesxxxyz8775 4 роки тому +1

    How the tank can topple over when angle is too large?

    • @Kidscancode
      @Kidscancode  4 роки тому +1

      You'd need to check the angle of the normal.

  • @jaxamus13
    @jaxamus13 4 роки тому

    Where did u go

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

    hey, wonderful tutorial. You wouldn't have an idea how we could do the same thing but with a rigid body?
    My rigid body spaceship, has a problem when using global_transform (Error setting property 'global_transform' with value of type Nil.)
    I'd appreciate some advice how to do what you did with a rigid body :)

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

      That error means you're trying to assign it a value of "null". Whatever variable you're trying to assign to `global_transform` doesn't have a value.

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

      @@Kidscancode thx for a reply, I understand that, I don't understand why that happens, I don't know how to learn how rigid body code works.

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

      @@linuxrant The best way to learn how rigid bodies work is to read the docs. docs.godotengine.org/en/stable/classes/class_rigidbody.html

  • @odysseycult
    @odysseycult 4 роки тому +1

    sup man, can you do a tutorial about json and make a save system like checkpoints ? I've been trying to figure it out but couldn't

    • @Kidscancode
      @Kidscancode  4 роки тому

      JSON shouldn't be used for saving data. godotrecipes.com/basics/file_io/

  • @ara7546
    @ara7546 4 роки тому +1

    i have a question about performance. Your kinematic object, does he get applied by engine physics ?

    • @Kidscancode
      @Kidscancode  4 роки тому

      I don't understand the question.

    • @ara7546
      @ara7546 4 роки тому

      @@Kidscancode do you use godot physics or bullets ?

    • @Kidscancode
      @Kidscancode  4 роки тому +1

      I'm using the default bullet physics.

    • @ara7546
      @ara7546 4 роки тому

      @@Kidscancode have you tried to switch off physics and use raycast only, i am asking myself if it will boost fps ?

  • @ferozakbar
    @ferozakbar 4 роки тому

    hello KCC, can you please share the source code of 2d version of this, im stuck at many places. thanks in advance

  • @tradingwithwill7214
    @tradingwithwill7214 4 роки тому +1

    Can you do Oculus Quest VR game with Godot for newbs? Thanks

    • @Kidscancode
      @Kidscancode  4 роки тому

      If you send me an Oculus Quest, I'll be happy to do so. :)

  • @UsernameDoesntCare
    @UsernameDoesntCare 3 роки тому

    this really didnt work for me, as when i tried it, my character rotated into the floor.

  • @OGTV1up
    @OGTV1up 4 роки тому +1

    yay im finaly one of the first person to type something in a video

  • @tomansionpercussion4389
    @tomansionpercussion4389 4 роки тому +1

    the camera is rotating too

  • @arielgimenez_95
    @arielgimenez_95 4 роки тому +1

    source code?

  • @mitchellstephens4228
    @mitchellstephens4228 4 роки тому +2

    Can someone please help me with this, this is the most frustrating experience of my life. Im on mac.
    Whenever I open Godot, it loads fine. When I select the project I want to work on, it opens a new Godot app that actually runs the file. Yeah, it works. But I have to keep moving one instance of the application to the trash every time I close the app. I end up having 3-4 instances of the Godot app in my toolbar for no reason.

    • @tomaszgoral8154
      @tomaszgoral8154 3 роки тому

      I think you should go to the godot community and ask there. You might find someone who fixed this problem, best of luck!

  • @Endgame901
    @Endgame901 3 роки тому +2

    I mean this is a year old now but
    Is there a reason you don't use multie raycasts around the player object and average the normal between them?

  • @qqq9542
    @qqq9542 4 роки тому +1

    Thank u so much!! Can this be applied to 2D as well?

  • @AnKlMa
    @AnKlMa 3 роки тому

    Do you have to do this manually in Godot?
    Like in the UPBGE there is a function called alignAxisToVect()
    Also in another video with the homing missiles you programmed the tracking manually.
    Isnt there a built-in function like trackTo() (which the upbge also has)?
    Don't get me wrong, I love to see what is going on behind the curtains and I never really thought about that

  • @kimiscool4999
    @kimiscool4999 4 роки тому +1

    Try this command on python
    import turtle
    fred = turtle.Pen()
    fred.shape("turtle")
    fred.speed(1)
    for i in range(4):
    fred.forward(100)
    fred.right(90)
    fred.up()
    fred.forward(200)
    fred.down()
    for i in range(4):
    fred.forward(100)
    fred.right(90)
    fred.up()
    fred.forward(200)
    fred.down()
    for i in range(4):
    fred.backward(100)
    fred.right(90)
    for i in range(4):
    fred.backward(100)

  • @McDADDyK
    @McDADDyK 4 роки тому +1

    can you reply to older video questions or set up a patreon so we can pay to get answers...

    • @Kidscancode
      @Kidscancode  4 роки тому +1

      I answer questions when I have time to answer questions. Spamming doesn't make it happen faster.

  • @jamesxxxyz8775
    @jamesxxxyz8775 4 роки тому +1

    You can use this ua-cam.com/video/Bn-y38Wd2rE/v-deo.html and ua-cam.com/video/kbNaACyqdAY/v-deo.html to create a tutorial(s). You can put a turret on this vehicle and continue how to shoot and rotate with it...

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

    The lion kingdom has found it very difficult to align with walls when sliding along walls. It seems it would take 2 more raycasts to detect the wall normals.

  • @OGTV1up
    @OGTV1up 4 роки тому

    and one more thing im 9 and right now on your python vids you should do some more i like them and i
    do them and have alot of fun with my mom and dad and if my brother was a few more years older i would be haveing
    fun with him to

  • @OGTV1up
    @OGTV1up 4 роки тому

    yep i do python oh and sorry for spam again im a kid

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

    Thank you for explaining the CROSS PRODUCT. I have googled for that for years and ended up getting pages that made me more confused that I was at the start of the search. Now could you explain the DOT PRODUCT, NORMALIZATION OF VECTORS and input.get_axis.STRENGTH ? That last one I think I understand I just do not know why some authors use it for both positive and negative values when the docs say it only returns BETWEEN 0 and 1 ?

  • @nabilabedkarim7112
    @nabilabedkarim7112 4 роки тому

    i got a problem. i wich i can get your help
    when i try to open godot this masseg show up :
    your video card driver does not support any of the supported opengl versions
    and i cant update my windows 7 because is pirated
    can you help me pls

  • @nabilabedkarim7112
    @nabilabedkarim7112 4 роки тому

    i got a problem. i wich i can get your help
    when i try to open godot this masseg show up :
    your video card driver does not support any of the supported opengl versions
    and i cant update my windows 7 because is pirated
    can you help me pls

  • @nabilabedkarim7112
    @nabilabedkarim7112 4 роки тому

    i got a problem. i wich i can get your help
    when i try to open godot this masseg show up :
    your video card driver does not support any of the supported opengl versions
    and i cant update my windows 7 because is pirated
    can you help me pls

    • @seubmarine5347
      @seubmarine5347 4 роки тому

      Your computer is too old

    • @nabilabedkarim7112
      @nabilabedkarim7112 4 роки тому

      @@seubmarine5347 ok.......

    • @seubmarine5347
      @seubmarine5347 4 роки тому

      @@nabilabedkarim7112 It can't open godot because it's too old, it doesn't have graphic card that suport opengl

    • @nabilabedkarim7112
      @nabilabedkarim7112 4 роки тому

      @@seubmarine5347 nah......
      i already fix it with some program
      but thanks anyway

  • @nabilabedkarim7112
    @nabilabedkarim7112 4 роки тому +1

    i got a problem. i wich i can get your help
    when i try to open godot this masseg show up :
    your video card driver does not support any of the supported opengl versions
    and i cant update my windows 7 because is pirated
    can you help me pls

  • @nabilabedkarim7112
    @nabilabedkarim7112 4 роки тому

    i got a problem. i wich i can get your help
    when i try to open godot this masseg show up :
    your video card driver does not support any of the supported opengl versions
    and i cant update my windows 7 because is pirated
    can you help me pls

  • @nabilabedkarim7112
    @nabilabedkarim7112 4 роки тому

    i got a problem. i wich i can get your help
    when i try to open godot this masseg show up :
    your video card driver does not support any of the supported opengl versions
    and i cant update my windows 7 because is pirated
    can you help me pls

    • @proatgaimz
      @proatgaimz 4 роки тому

      There's free operating systems like Linux if you don't want to pay for an operating system

    • @nabilabedkarim7112
      @nabilabedkarim7112 4 роки тому

      @@proatgaimz very good but can you explaine more please