0 votes

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.

in Engine by (15 points)

1 Answer

+1 vote
Best answer

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 physicsprocess(delta):
    (indent)path = getparent().getnode("Path2D/PathFollow2D")
    (indent)path.offset += speeddelta
    indent)globaltransform = path.globaltransform
    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.

by (3,875 points)
selected by

Thanks a ton. Path2D and PathFollow2D did the trick.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.