Create 2D Game with Auto Driving Car

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By G T Manoj

I’m new to GoDot Game Engine. I’m trying to create a 2D game in which I want to move a 2D car object on a curve route programmatically. What is the right node to be configured to achieve this, I’m trying to use VehicleBody which is a 3D node, is this the right way to do.

:bust_in_silhouette: Reply From: SIsilicon

Since you’re new to Godot I suggest you step away from what are called spatial nodes for now(3D nodes like Vehicle Body). When you said “move a 2D car object on a curve route programmatically”, you meant on its own in a constant path? If so, then Path2D and PathFollow2D are just what you need. Follow these steps.

  1. Add a Path2D node to your scene.

  2. Create a path using said node. Notice the new buttons on the top? The one with the plus symbol makes add a point by clicking on the point in the editor where you want it.

  3. Once your done creating a path of points, create a PathFollow2D as a child of this node(to do that just add a node while the other node is selected).
    Now it’s time for your car.

  4. Assuming you’re car interacts with physics, create a kinematicBody2D node first. And as a child of that, a CollisionShape2D and a Sprite. The Sprite’s texture will be an image of your car, and your CollisionShape2D should have a collision shape of whatever you want. Preferably a capsule.

  5. Give your car(the KinematicBody2D node) a gdscript. You do know what a gdscript is right?

  6. In the GDscript remove everything and type the following-

extends KinematicBody2D

var speed = 20
func _physics_process(delta):
(*indent*)path = get_parent().get_node("Path2D/PathFollow2D")
(*indent*)path.offset += speed*delta
(*indent*)global_transform = path.global_transform

Note: don’t type (indent). Make an indent. And speed is how fast your car is moving in pixels per second.

That should be all. If you run the game then your car should follow along the path you specified.

Thanks a ton. Path2D and PathFollow2D did the trick.

G T Manoj | 2018-05-13 10:06