+3 votes

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 by (18 points)

2 Answers

+1 vote
Best answer

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.add_point(pos+Vector2(r,0),Vector2(0,-r))
CircularMovement.add_point(pos+Vector2(0,r),Vector2(r,0))
CircularMovement.add_point(pos+Vector2(-r,0),Vector2(0,r))
CircularMovement.add_point(pos+Vector2(0,-r),Vector2(-r,0))
CircularMovement.add_point(pos+Vector2(r,0),Vector2(0,-r))
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?

+5 votes

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 Radius = 100
var _centre
var _angle = 0

func _ready():
    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 (90 points)
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.