This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+3 votes

Hi again,

basically, I need to detect a collision inside the script of a KinematicBody2D and destroy both the colliding objects (it's a bullet that hits an enemy).
In 2.1 I used:

func _fixed_process(delta):
var movement = direction.normalized() * speed * delta
move(movement)
if(is_colliding()):
    var entity = get_collider()
    entity.queue_free()

But now it looks like the methods getcollider and iscolliding don't exist anymore.

I looked at the documentation and I saw there's a moveandcollide method, but I can't figure out how to use it.
Can anyone tell me how to replicate that code in 3.0?

in Engine by (569 points)

1 Answer

+4 votes
Best answer

move_and_collide() is the replacement for move(). There is also move_and_slide() if you want sliding behavior.

move_and_collide() returns an object, a KinematicCollision2D, which contains the info about the collision. Your code would change to:

func _physics_process(delta):
    var collision_info = move_and_collide(direction.normalized() * speed * delta)
    if collision_info:
        collision_info.collider.queue_free()

Note that _fixed_process has changed to _physics_process. See the docs here for more info:
http://docs.godotengine.org/en/latest/classes/class_kinematiccollision2d.html

by (22,191 points)
selected by

Thanks, apparently this method does not exist in my version (I downloaded the 3.0 version at this link). Maybe that's why I could not use _physics_process .
I guess I'll have to build one on my own.

Yes, a great many things have changed since Alpha 1 was released. If you're going to work with 3.0, I recommend you build your own periodically, or get one of Calinou's builds here: https://bintray.com/calinou/godot/editor/

I've been trying to download a build from that link, but I get "Forbidden!" every time, I don't know why.

Isn't the delta already supplied by the method?

Not move_and_collide(). You're thinking of move_and_slide().

func _physics_process(delta):
    var collision_info = move_and_collide(direction.normalized() * speed * delta)
    if collision_info:
        collision_info.collider.queue_free()

what do you mean by "if collision_info:" ?

move_and_collide() returns either null (if no collision) or a KinematicCollision2D (if there is one). Therefore, we can test for that return value to process the collision.

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.