How to create snake-like enemies in Godot?

Godot Version

`Godot 4.3

Question

`Hello everyone! I’m new to programming and to Godot, and something I’ve been struggling to find resources on is how to recreate classic 2D snake-style enemies. Does anyone know of a tutorial or what this type of enemy is called?

`

Check your post formatting. The image doesn’t show.

1 Like

This is a White Dragon from Castlevania: Symphony of the Night. They come in two varieties: with their tail bound to a wall, or freely bouncing around.

Your question is very broad and multiple implementations are possible. I have never created a ‘snake-like enemy’ before, but if I had to, my first naive try would be to make two new scenes: a head scene, and a segment scene.

The head scene has a collision shape, marker2d, rigid body, and a Sprite2D. It would have a basic script attached like this:

# head.gd

@export var segment : Segment = null
@export var next_segment_marker : Marker2D = null
@export var head_sprite : Sprite2D = null

func _ready()->void:
    if segment:
        segment.position = next_segment_marker.position

func _physics_update(delta : float)->void:
    pass 
    # Implementation is left up to you. 
    # If you want it to be like in the Castlevania games, check if the head is touching the ground. 
    # If it is, add an upward impulse. If it is in the air, let gravity pull it down.

Find a nice sprite of a snake head and assign it via the inspector. Then assign the Marker2D from inside the scene and drag it to the back of the skull.

The segment script is the body of the snake. It should also have a rigid body (careful, make sure it is set not to collide with the snake head or other segments.) It should also have a sprite2D.

# segment.gd
@export var segment : Segment = null
@export var next_segment_marker = null

func _ready()->void:
    if segment:
        segment.position = next_segment_marker.position

Assign a sprite (the snake’s body), and move the marker to where you would like the next segment to line up.

Now make a new snake scene. Make a head node the scene’s root. Add as many segment nodes to the scene as you would like, then daisy chain them together via the inspector. i.e. go to the head, and (via the inspector), assign one of the segment nodes inside the scene. Do the same for all segments. Run the scene, and the head should bounce up and down with the segments following along.

All of this code is untested and the snake doesn’t actually move anywhere yet, so there’s a lot left to implement and fix.

e: another thing to keep in mind: the SotN sprite rotates body segments. This implementation does not; at least not in a controlled manner. You would probably also have to look into that.

2 Likes

Thank you very much for the super complete answer! I will definitely test this solution, you helped a lot!

Ok, this was my first post here, I’ll check what I did wrong.

Don’t sweat it. We’ve all gone through that. :smiley:

You have a preview of your post on the right-hand side panel. Use it to proof your post. Not everyone will take the time to dig the info out of a post to help others.

Also, welcome!