KinematicBody2D is a node type in the Godot Engine used for 2D game development. It's designed to provide a way to have objects that can move and interact with other objects within the game world while having custom collision response written by the developer without relying on the physics engine's simulation.
Here's how you can use
Adding a KinematicBody2D Node:
In the Godot editor, you can add a
KinematicBody2D to your scene by selecting "Add Child Node" (the "+" icon) and searching for
KinematicBody2D. After adding it, you attach scripts to this node to define its behavior.
You control the movement of a
KinematicBody2D using the
move_and_collide() methods in GDScript within the
_physics_process(delta) function. Here's an example:
After moving the body using one of the above methods, Godot provides information about collisions, available through methods such as
get_slide_collision(). You can use this information to detect when and what the
KinematicBody2D has collided with.
Setting Up Collisions:
To physically interact with other nodes, a
KinematicBody2D must have one or more
CollisionPolygon2D children defining its shape.
Finding KinematicBody2D in Code:
If you need to find a reference to a
KinematicBody2D node in code, you can use the
get_node() function or the
$ shorthand syntax if you know the node path:
KinematicBody2D is essential for creating controlled movements like players or certain types of enemies. It gives you full control over motion and collision response, making it a centerpiece for many types of 2D games developed in Godot.