我相信这里的问题是你没有使用碰撞。该功能move_and_slide
或move_and_collide
将接合一个运动体移动,并撞击其他体(包括运动体)。
但是,一旦您使用任一功能,您就会意识到它们不会以您在问题中需要的方式相互推动。
来自文档(en,fr)
move_and_collide
:移动的物体会移动,如果有碰撞器(任何障碍物),它会在碰撞时立即停止并返回一个KinematicCollision2D
物体。
move_and_slide
:与 相比,采取进一步move_and_collide
的行动,移动的物体将滑过对撞机,将其视为墙壁或地板。对平台游戏很有用。
在您的用例中,您别无选择,只能自己控制碰撞。您可以在此处获得有关 KinematicCollision2D 的更多信息:( en , fr )。这可能是你这样做的方式:
var collision = player.move_and_collide(direction_vector)
if (!collision):
# No collision occurred
return
else:
# Collided with a wall or player
if (collision.collider == wall)
# if you keep track of wall object as a variable to identify it
# you can have the sumo just stop moving and not process anymore
return
else:
var opponent = collision.collider
# write game logic to move opponent and yourself
return