How can I transfer the linear velocity from one KinematicBody2D to another?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robeboyg

I want to make a Sumo game where two players in one plataform beat each other with the builded velocity but I don’t know how to transfer one body’s velocity to the other

:bust_in_silhouette: Reply From: njamster

You haven’t posted any code, so the answer will be rather vague, but: once both players collide (note that move_and_collide returns a KinematicCollision2D), you access the velocity of the collider and set the bodies own velocity to that value:

extends KinematicBody2D

var velocity = Vector2()

func _physics_process(delta):
    # ...
    var collision = move_and_collide(velocity * delta)
    if collision:
        velocity = collision.collider.velocity
    # ...

If there are other bodies a character can collide with, you’ll need to ensure that the collider is actually a player character. If you add them to a group, you can do:

if collision.collider.is_in_group("sumo"):
    # ...