How can I get a KinematicBody2D to detect collision?

FactoryBuilderFactoryBuilder Posts: 1Member

I tried the documentation. It was kinda confusing. I've got a ball which I want to move_and_collide(moveBy) where moveBy = Vector2(1,1). Is there a way to detect when this ball's CollisionShape2D hits another CollisionShape2D? Also, when it does, is there a way to change just the y component of moveBy without altering the x component?


  • uaknightuaknight Posts: 42Member
    edited November 2019

    Altering the y and not the x, that looks like something you'll need the bounce functionality for. The example found here might work right out of the box for you (see the section about KinematicBody2D):

    I'm using it myself in my project. The local Vector2 variable velocity is used as an intermediate variable. It can be changed manually in the _process event, if required. The actual movement (with an eventual, additional collision) takes place in the _physics_process event. Works fine.

    var velocity = Vector2(1, 1)
    func _physics_process(delta):
        var collision = move_and_collide(velocity * delta)
        if collision:
            velocity = velocity.bounce(collision.normal)

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file