0 votes

I'm currently using KinematicBody2D to implement my Player and Enemy classes. The Enemy decides how it should move using a behavior tree, and I call move_and_collide inside one of the leaves of the tree.

I was pretty happy with the way that this was working until I started to try and implement some of my other game mechanics. I have an Area2D that tries to pull the KinematicBody2D towards its center, by calling move_and_collide every frame on any body that has entered the area.

This results in (at least) two calls to move_and_collide per frame -- one where the Enemy tries to move away from the center of the Area2D, and another where the Area2D tries to move the Enemy back towards its center.

Is it safe to structure things like this, or is there some built-in assumption in the way Godot works that requires me to restructure things such that move_and_collide is only called for a given KinematicBody2D once per frame?

Godot version 3.2.1
in Engine by (32 points)

1 Answer

0 votes
Best answer

Yes, it is safe to call move_and_collide multiple times per physics frame. This is actually how another built-in function, move_and_slide, works. It has argument named max_slides (default value is 4) that defines how many times per one call move_and_collide can be called (if during movement object collides with something).

by (1,650 points)
selected by
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 Frequently asked questions and 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 [email protected] with your username.