Distance constraints

Godot Version

Godot 4.3

Question

Hi I am trying to figure out distance constraints.
I want to make an enemy in a top down game that has segments that follow the head around but that will stay a certain distance away from it. Like Moldorm in Links Awakening for example.
Thank you.

I think this video should help:

1 Like

Would you give me an example in code please?

You just need to implement the segment class of the video:


class Segment:

    var x
    var y
    var angle
    var distance

    func update(previous_segment: Segment):
        var a: float = atan2(prev.y-y, prev.x-x)
        angle = a
        float d = sqrt(pow(prev.x-x, 2)+pow(prev.y-y, 2))
        if d > distance:
            var delta: float = d - distance
            x += delta*cos(angle)
            y += delta*sin(angle)

add this into your enemy class and then create a segment for every segment of your character

Thank you so much! God bless.

Did you manage to implement it?

It doesn’t seem to know what “prev” means.

yeah i renamed it. Replace “prev” with “previous_segment”

Gotcha

So if I’m right I get the head to call the segment class right?

Also do I need to plug the x and y variables into some movement code?

the x and y coordinates are for the sprites of the segments that are supposed to follow. You might have to subtract the heads global_position from it, im not sure about it