Hi everyone!! :)

I need to create a script to make the move of an enemy. The enemy is a seagull. The seagull flies with circular and continuous motion. I need to use move function because the seagull needs to detect collisions. I dont know if in godot I can use something similar to euler function like Unity 5 in this case.

Ideas??

Thanks for any help ;)

in Engine

+1 vote

First you need to declare circular movement. Like:
EDITED:

``````var speed = 1
var CircularMovement = Curve2D.new()
var r = 100
var pos = Vector2(200,200)
CircularMovement.set_bake_interval(1)
``````

Then you should save somewhere position in baked_points, eg.:

``````var bppos = 0
``````

If you can use `move_to()` instead of `move()`, everything will be easier. If so, use:

``````func _fixed_process(delta):
if bppos + delta*speed >= CircularMovement.get_baked_points().size():
bppos += delta*speed - CircularMovement.get_baked_points().size()
else:
KinematicBody2D.move_pos(CircularMovement.get_baked_points()[round(bppos)])
bppos += delta*speed
``````

SHOULD work, but not best solution.

by (186 points)
selected by

This is bad due to ignoring the delta value of the fixed process function.

Yup, that's true. It should be something like this:

``````KinematicBody2D.move_pos(CircularMovement.get_baked_points()[round(bppos)])
bppos += delta*1
``````

And o/c condition: if bppos is at the end of curve; then pbpos = 0
Thanks for caution :)

Thanks for editing! Now this should work find on any computer / framerate.

Many thanks I will try it :)

Hey, thanks! That was really helpful :)

Do you know how can I change the points to get an elliptical orbit instead of a circular one?

I know this has been answered, but I found a simpler solution while searching around that I think might be useful for someone.

``````var RotateSpeed = 5
var _centre
var _angle = 0

set_process(true)
_centre = self.get_pos()

func _process(delta):
_angle += RotateSpeed * delta;

var offset = Vector2(sin(_angle), cos(_angle)) * Radius;
var pos = _centre + offset
self.set_pos(pos)
``````

And just replace the last line "self.set_pos(pos)" for move(pos) if you are using a KinematicBody2D.

by (93 points)