Topic was automatically imported from the old Question2Answer platform.
Asked By
DodoIta
Old Version
Published before Godot 3 was released.
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 get_collider and is_colliding don’t exist anymore.
I looked at the documentation and I saw there’s a move_and_collide method, but I can’t figure out how to use it.
Can anyone tell me how to replicate that code in 3.0?
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:
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.
I’ve been trying to download a build from that link, but I get “Forbidden!” every time, I don’t know why.
DodoIta | 2017-10-18 22:40
Isn’t the delta already supplied by the method?
Zakkle | 2018-04-26 13:58
Not move_and_collide(). You’re thinking of move_and_slide().
kidscancode | 2018-04-26 14:07
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:” ?
OmarSbeta | 2018-09-10 14:03
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.