简介

移动时KinematicBody2D,您不应直接设置其position属性。而是使用move_and_collide()或 move_and_slide()方法。这些方法沿给定矢量移动物体,如果检测到与另一个物体的碰撞,则立即停止。KinematicBody2D发生碰撞后,必须手动编码任何碰撞响应。

它们都是KinematicBody2D的方法。为了管理运动物体或角色的逻辑,总是建议使用物理过程_physics_process(),因为它在物理步骤之前被调用并且它的执行与物理服务器同步,它也被称为每秒相同的次数。 这使得物理和运动计算以比使用常规过程更可预测的方式工作,如果帧速率太高或太低,则可能具有尖峰或丢失精度。

move_and_collide(参数:每帧像素位移)

Movement and collision:意思是移动和碰撞
这个方法有一个 Vector2参数以表示物体的相对运动。 通常,这是您的速度向量乘以帧时间步长(delta)。 如果在沿着此向量方向的任何位置,引擎检测到碰撞,则物体将立即停止移动。 如果发生这种情况,该方法将返回 KinematicCollision2D 对象。
KinematicCollision2D是一个包含有关碰撞和碰撞对象的数据的对象。使用此数据,您可以计算碰撞响应。


extends KinematicBody2D

const GRAVITY = 200.0

# velocity 定义为每秒像素位移
var velocity = Vector2()

func _physics_process(delta):
    velocity.y += delta * GRAVITY

    var motion = velocity * delta
    move_and_collide(motion)

move_and_slide(参数1:每秒像素位移 , 参数2:地板法线的矢量方向 , 参数3...)

Vector2 move_and_slide(linear_velocity: Vector2, up_direction: Vector2 = Vector2( 0, 0 ), stop_on_slope: bool = false, max_slides: int = 4, floor_max_angle: float = 0.785398, infinite_inertia: bool = true)

Moves the body along a vector. If the body collides with another, it will slide along the other body rather than stop immediately. If the other body is a KinematicBody2D or RigidBody2D, it will also be affected by the motion of the other body. You can use this to make moving or rotating platforms, or to make nodes push other nodes.
沿矢量移动实体。如果物体与另一物体相撞,它将沿着另一物体滑动而不是立即停止。如果另一个物体是一个运动模块2d或刚体2d,它也会受到另一个物体运动的影响。您可以使用它来创建移动或旋转平台,或使节点推动其他节点。

This method should be used in Node._physics_process() (or in a method called by Node._physics_process()), as it uses the physics step's delta value automatically in calculations. Otherwise, the simulation will run at an incorrect speed.
此方法应在Node._physics_process()中使用(或在Node._physics_process()调用的方法中使用),因为它在计算中自动使用物理步骤的delta值。否则,模拟将以错误的速度运行。

linear_velocity is the velocity vector in pixels per second. Unlike in move_and_collide(), you should not multiply it by delta — the physics engine handles applying the velocity.
线性速度是以像素每秒为单位的速度矢量。与move_and_collide()不同,不应将其乘以delta—物理引擎处理渲染的时间。

up_direction is the up direction, used to determine what is a wall and what is a floor or a ceiling. If set to the default value of Vector2(0, 0), everything is considered a wall. This is useful for topdown games.
up_deriction方向是指地板的法线方向,用于确定什么是墙,什么是地板或天花板。如果设置为Vector2(0,0)的默认值,则所有内容都视为墙。这对自上而下的游戏很有用。

If stop_on_slope is true, body will not slide on slopes when you include gravity in linear_velocity and the body is standing still.
如果`stop_on_slope是真的,当你将重力纳入线性速度并且身体静止时,身体不会在斜坡上滑动。

If the body collides, it will change direction a maximum of max_slides times before it stops.
如果物体发生碰撞,它将在停止前改变方向最多滑动几次。

floor_max_angle is the maximum angle (in radians) where a slope is still considered a floor (or a ceiling), rather than a wall. The default value equals 45 degrees.
最大倾斜角是最大角度(以弧度为单位),其中斜坡仍被视为地板(或天花板),而不是墙壁。默认值为45度。

If infinite_inertia is true, body will be able to push RigidBody2D nodes, but it won't also detect any collisions with them. If false, it will interact with RigidBody2D nodes like with StaticBody2D.
如果无限惯性是真的,物体将能够推动刚体2维节点,但它也不会检测到与它们的任何碰撞。如果为false,它将与RigidBody2D节点交互,就像与StaticBody2D交互一样。

Returns the linear_velocity vector, rotated and/or scaled if a slide collision occurred. To get detailed information about collisions that occurred, use get_slide_collision().
返回线性速度向量,如果发生滑动碰撞,则旋转和/或缩放。若要获取有关发生的冲突的详细信息,请使用get_slide_collision()。

注解:
移动和滑动。实际是move_and_collide()的升级版,任何该方法能做的事情,move_and_collide()也能做到,只是需要额外的代码。
const GRAVITY = 200.0

# velocity 定义为每秒像素位移
var velocity = Vector2()

func _physics_process(delta):
    velocity.y += delta * GRAVITY
    velocity =  move_and_slide(velocity)
最后修改:2020 年 03 月 25 日
如果觉得我的文章对你有用,请随意赞赏