Godot Recipe: 3D Kinematic Car

Поділитися
Вставка
  • Опубліковано 5 вер 2024
  • We've gotten a lot of questions about how to make a 3D car game. Here
    we'll cover the basics of setting up a kinematic car.
    Text version & link to download the sample project:
    godotrecipes.co...
    Support me on Patreon: / kidscancode
    Art from Kenney:
    kenney.nl/asse...
    VehicleBody tutorial by Bastiaan Olij: • Godot Vehicle Tutorial...

КОМЕНТАРІ • 90

  • @anindyaroythetechmaster
    @anindyaroythetechmaster 3 роки тому +12

    Was just thinking about adding a car in my FPS, and here is the tutorial

  • @jorgegomes83
    @jorgegomes83 3 роки тому +27

    Good to see a new Godot Recipe!
    Thanks!

  • @danielkeenan2508
    @danielkeenan2508 3 роки тому +15

    Great tutorial. Looking forward to more like adding drifting.
    When recording future episodes you may want to consider turning on "Distraction Free Mode" for the script editing segments. It would make it easier to see all the code without scrolling about etc.

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

    Upload from my favourite teacher on my birthday! What a nice present!

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

    How would you add a gradual turning for when the player uses the keyboard?
    EDIT: well, I managed to kinda hack it in. I don't like it much, but it works. Code below, if anyone needs this:
    *var steer_speed = 2*
    *func get_input(delta):*
    *var turn = Input.get_action_strength("steer_left") - Input.get_action_strength("steer_right")*
    *var steer_angle_degs = rad2deg(steer_angle)*
    *var steer_factor = steer_speed*delta*
    *if turn > 0: steer_angle_degs = lerp(steer_angle_degs, steering_limit, steer_factor)*
    *elif turn < 0: steer_angle_degs = lerp(steer_angle_degs, -steering_limit, steer_factor)*
    *else: steer_angle_degs = lerp(steer_angle_degs, 0, steer_factor)*
    *steer_angle = deg2rad(clamp(steer_angle_degs, -steering_limit, steering_limit))*

  • @HomeofLawboy
    @HomeofLawboy 3 роки тому +3

    Just a minor nitpick for ~18:14, if we're not using the magnitude of the dot product, we don't need to normalize the velocity and we save ourselves a square root.
    Excellent tutorial nonetheless!

    • @Kidscancode
      @Kidscancode  3 роки тому +5

      I haven't worried about such things for years. The cost of a square root is insignificant in today's processors. If you find yourself in a situation where such micro-optimizations are important, sure, go for it.

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

    can any of you paste the code in comments

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

    Thanks a lot! Looking forward to the series to build it up more and more!

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

    Thank you very much for your educational sharing. I wish you Health and Success

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

    why i cannot stop the car, after braking it keeps me forward and i canot stop the car , if i reverse i cant do accelerate forward

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

    I know this tutorial was a while back ago so it might be the deference's in versions but for some reason the car doesn't erase it velocity almost. If I start running it and accelerate then try to reverse, eventually the car starts to stop but will still go forward very slowly. The same happens if I try to reverse the car first. Can anyone help me with this?

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

      Mine does the same did you fix it?

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

      @@ryscale4014 no unfortunately not. I ended up shelving the project to work on some other time. Sorry mate

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

      @@cosmicfruit7111 Thank you for replying.

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

      * Change true in velocity variable in _physics_process to false
      func _physics_process(delta):
      if is_on_floor():
      get_input()
      apply_friction(delta)
      calculate_steering(delta)
      acceleration.y = gravity
      velocity += acceleration * delta
      => velocity = move_and_slide_with_snap(velocity, -transform.basis.y, Vector3.UP, true)
      to
      velocity = move_and_slide_with_snap(velocity, -transform.basis.y, Vector3.UP, false)
      * And play around with the drag , friction, engine power and max_speed_reverse variable. For me the following worked
      export var gravity = -20.0
      export var wheel_base = 0.6
      export var steering_limit = 10.0
      export var engine_power = 20
      export var braking = -9.0
      export var friction = -10.0
      export var drag = -5.0
      export var max_speed_reverse = 10.0

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

    not sure what i did wrong, triple check the code. But velocity.length() is throwing a "nonexistant function 'length' in base 'float' error. In fact both Acceleration and Velocity aren't recognized as a vector3
    EDIT: figured it out, spelled tranform instead of transform. Not even sure how it did that

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

    I really like the look of this implementation, especially because I can really feel the impact the controls have by simply watching the footage of you driving around, but why implement it this way from scratch and not use a VehicleBody Node? With a bit of tweaking it seems this can achieve similar results but with the advantage of full vehicle body physics. Of course your method is a bit easier to get started with for simple projects but the built in node seems way more flexible, especially in case of collision detection and implementing multiple different vehicles.
    Edit: Okay, I just tried the built in node an now I understand why you implement it that way. I was not aware that this method results in failsafe car handling like in Super Mario Kart while the built in VehicleBody results in a more realistic car driving experience like in GTA.

    • @Kidscancode
      @Kidscancode  3 роки тому +3

      I addressed this in the demo. VehicleBody is fine, but being RigidBody based, has the same tradeoffs versus KinematicBody. As I mentioned, if you want an arcade-style feel, and don't need more complex simulation with suspension, etc. then you don't need VehicleBody. It all boils down to what you need for your game.

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

    Hi, did anyone got this error while running in F6 mode? I got it, the car can still run but I can't steer, the wheels do rotate but the car itself doesn't. I have copied every exact word of the script down from the doc.
    When I clicked on it, it returned me to the line "front_wheel += velocity.rotated(transform.basis.y, steer_angle) * delta" in func calculate steering
    Error: set_axis_angle: The axis Vector3 must be normalized.
    EDIT: Ok I found the solution, you can just stick .normalized after transform.basis.y, turning the line to "front_wheel += velocity.rotated(transform.basis.y.normalized(), steer_angle) * delta"

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

      $tmpParent/race/wheel_frontLeft.rotation.y = deg2rad(180)+(steer_angle*2) in the get input helped me

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

      @@louisjoseph7869 What's the error though?

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

      @@spaghettiman512 my bad i thought u had the problem of the frontleft wheel not being alingned. the model had used the same wheels inverted so the leftwheel in the front would look like the right wheel.

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

    Hlo.. I didnt understand chase camera... Plz help rest of all working

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

    Hello thank you for the tutorial, is this godot 3.5.3 ? I did not fully watch it

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

    Thank you so much. Been waiting for this for so long. Can't wait to try It.

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

    Somehow missed this video of yours, pretty neat!
    You probably already know this but if you add `class_name CarBase` to your car_base.gd script you can now refer to it by name instead of the path to the script and thus do `extends CarBase` in your cars.

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

      Yeah, looking back I'm not sure why I didn't do that at the beginning, and then I just didn't think about it...

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

      @@Kidscancode i don’t think many people are used to it, it doesn’t stand out as an option and it was only introduced in 3.1 I think so its new

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

    Nice video i love the explanation! Your vids helped me a lot. I just have one question when is drifting part tutorial gonna come out i cant wait!

  • @oriol420
    @oriol420 3 роки тому +3

    Great tutorial!!!

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

    cool bro. very cool. I'll definitely try this at some point.

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

    How about gears ⚙️ ? ??!

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

      I want to have a gear system for my car game too, for a more realistic driving feel

  • @1u8taheb6
    @1u8taheb6 3 роки тому

    Why is there no way to SNAP a CollisionShape to the center of another object? I hate eyeballing things, especially for such simple tasks.

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

      You can make it child of the object and set the position to (0, 0, 0) and then move it outside the object again, and it will stay at the exact position of the object.

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

    These are great videos - thank you. Totally dumb question: how the heck do I turn on the selector lugs when using collision shape? They just don't come on - also can only seem to get shapes to align via a grid even though I feel like I have turned snapping off

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

    How can you tilt the car in the X axis when accelerating or braking?

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

    16:45 Wait, did you set the friction and drag to affect the acceleration instead of the velocity?

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

    Thanks for this

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

    Just saw your video on Odysse and came here to leave a like aswell.

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

    Hey very helpful tutorial but can you zoom out the code next time? It would be way easier to see

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

    how does this work with obj?

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

    Thank you so much

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

    hey, so a few errors popped up, and you dont have them, any idea why? I followed your exact steps 'n stuff

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

      Of course I have no idea, because you didn't even say what the errors were. If you followed the steps exactly, there would be no errors, so I can only suggest that you go back and review what you've done, and/or compare with the example project.

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

    How do you make it breakable?

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

    love it!!

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

    Thanks a lot ! But there is a problem in my computer , every time I import the car it doesn't have color , its just plain black . Is there a solution for this ? Thanks

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

      are you using a intel graphics if so try switching you project to GLES 2

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

      @@_WeHaveFun_ I already did that as my computer doesn't run Godot without GLES 2

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

    How are you clamping the forward velocity? When I run the code as you have it written my car is eventually infinitely fast.

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

      The friction and drag set an upper limit on speed.

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

      @@Kidscancode something about this whole project was really weird. I need to restart and try again. My directions were scuffed at first (positive y was forward) and the backwards velocity was infinitely fast. I fixed the directionality and now forward is infinitely fast. I'm gonna start over and try again because neither friction nor drag seemed to be doing anything.

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

    Can you just send the project.godot file and gd file please

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

    Awesome !

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

    Adultz kan haz kode two! 🎉

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

    You are hero

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

    'unexpected identifier' :-( makes me wanna switch to unreal.

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

    How i put kenney assites in godot

  • @josef-c-6126
    @josef-c-6126 3 роки тому

    This solution works, but colliding with obstacles or AI will be hard to solve to look realistic. Why Kinematic type for the physics-based game?
    I think that VehicleBody is much easier to use in an arcade game like this. It is not too hard to set up and the final result is much better.

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

      I never said this was for a physics-based game, and we're not going for "realistic". I've done both, and for this particular application, I like the control KinematicBody gives me. Yes, VehicleBody solves some issues, but it raises others, just like any RigidBody vs KinematicBody tradeoff.
      I should probably do a VehicleBody demo at some point to highlight the differences. (adds to list)

    • @josef-c-6126
      @josef-c-6126 3 роки тому

      ​@@Kidscancode I apologize for that word, I mean "realistic" compared to "Mario kart" games.
      Every racing game has some kind of physics interaction between cars and world environment. And that's where I see the problem. From my experience, the kinematic body is great for characters - move and play an animation. But the racing car needs to bounce off the walls and reacts with other cars. And you are using move and slide for movement. How you plan to handle collisions?

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

      It’s best practice to reduce the amount of randomness in Arcady games to make it as fun as it can possibly be. Collision could easily be solved by playing a cartoony animation for when 2 cars bump into each other. But normally that isn’t fun so I don’t it’s a good idea.

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

      ​@@Kidscancode I have to agree with the concerns about the collisions, though. They're too sticky for games like Outrun 2 or GTA 2, for example. If you touch a wall from the side, the front will turn into the wall and stop your car. I'm not sure where the problem may be, though. Maybe it's the *move_and_slide* function, or maybe there's something else we could do there to fix this.
      I'd like to point out, though, that RigidBody has a kinematic mode, and it can also be controlled by a "custom integrator". Might be worth keeping in mind. Personally, I don't know enough to know what to do.
      Shifty (the Qodot dev) wrote about this, regarding FPS character controllers, but I suppose the same notions could be extrapolated to cars: markdownpastebin.com/?id=d9d61e67f9d64db2bd215f165b931449

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

    i love you

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

    How did you get car base.gd?????

  • @itsME-dc4vm
    @itsME-dc4vm 3 роки тому +1

    Nice ;V

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

    for some reason I can only turn when going in reverse
    can anyone help me with this

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

      I know it's a little late, but in the hopes someone else needs a solution, I had this issue, and the issue was the "look_at(trans..." line was still indented as if part of the "if d < 0:" statement.

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

    good tutorial!!!!!!!!!!

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

    Very nice explanation and approach. I'm beginning to think I'm the only person that uses z positive as forward. :) I just don't like -z...

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

      I remember talking to one person who insisted on rotating everything so that Z was up. It just means they had to do extra work. If it works for you, I say go ahead, it's your game! :)

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

    where tf in the folder is "car_base_gd"

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

      lol you have to create it

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

    the car is not stops

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

      Is CharacterBody3D's movement mode set to 'Floating'?

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

      @@oktayyildirim2911 yes

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

    I am 13

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

    please please please make for us tutorial =car enter exit= !!!)))

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

    First

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

    Look, i get this is pretty old. But I've got something I am literally incapable of implementing for some reason despite it being so easy on most KinematicBody player controllers. How would you recommend using get_floor_normal() to ensure it can go up hills? My implementations have not done anything at all