KinematicBody

Inherits: PhysicsBody < CollisionObject < Spatial < Node < Object

3D 运动学物体节点。

描述

运动体是特殊类型的物体,指在让用户控制。它们完全不受物理影响;对于其他类型的物体,如角色或刚体,它们与静态体是一样的。然而,它们有两个主要用途。

模拟运动:当这些物体被手动移动时,无论是从代码还是从 AnimationPlayer(将 AnimationPlayer.playback_process_mode 设置为“physics”),物理将自动估算其线性和角速度。这使得它们对于移动平台或其他 AnimationPlayer 控制的物体非常有用,比如门、能打开的桥等。

运动学角色:除了可以执行碰撞检测之外,KinematicBody 也有用于移动对象的 API(move_and_collidemove_and_slide 方法)。这使得它们在实现对世界进行碰撞,但不需要高级物理的角色时非常有用。

教程

属性

bool

axis_lock_motion_x

false

bool

axis_lock_motion_y

false

bool

axis_lock_motion_z

false

float

collision/safe_margin

0.001

bool

motion/sync_to_physics

false

bool

move_lock_x

false

bool

move_lock_y

false

bool

move_lock_z

false

方法

bool

get_axis_lock ( BodyAxis axis ) const

float

get_floor_angle ( Vector3 up_direction=Vector3( 0, 1, 0 ) ) const

Vector3

get_floor_normal ( ) const

Vector3

get_floor_velocity ( ) const

KinematicCollision

get_last_slide_collision ( )

KinematicCollision

get_slide_collision ( int slide_idx )

int

get_slide_count ( ) const

bool

is_on_ceiling ( ) const

bool

is_on_floor ( ) const

bool

is_on_wall ( ) const

KinematicCollision

move_and_collide ( Vector3 rel_vec, bool infinite_inertia=true, bool exclude_raycast_shapes=true, bool test_only=false )

Vector3

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

Vector3

move_and_slide_with_snap ( Vector3 linear_velocity, Vector3 snap, Vector3 up_direction=Vector3( 0, 0, 0 ), bool stop_on_slope=false, int max_slides=4, float floor_max_angle=0.785398, bool infinite_inertia=true )

void

set_axis_lock ( BodyAxis axis, bool lock )

bool

test_move ( Transform from, Vector3 rel_vec, bool infinite_inertia=true )

属性说明

  • bool axis_lock_motion_x

Default

false

Setter

set_axis_lock(value)

Getter

get_axis_lock()

锁定物体的X轴运动。


  • bool axis_lock_motion_y

Default

false

Setter

set_axis_lock(value)

Getter

get_axis_lock()

锁定物体的Y轴运动。


  • bool axis_lock_motion_z

Default

false

Setter

set_axis_lock(value)

Getter

get_axis_lock()

锁定物体的Z轴运动。


  • float collision/safe_margin

Default

0.001

Setter

set_safe_margin(value)

Getter

get_safe_margin()

在运动函数中用于碰撞恢复的额外边距,参阅move_and_collide, move_and_slide, move_and_slide_with_snap

如果物体离另一个物体至少这么近,它就会认为它们正在发生碰撞,并在执行实际运动之前被推开。

一个较高的值意味着它在检测碰撞时更加灵活,这有助于持续检测墙壁和地板。

一个较低的值迫使碰撞算法使用更精确的检测,所以它可以用于特别需要精确性的情况,例如在非常低的比例下避免可见的抖动,或者用于运动体堆叠的稳定性。


  • bool motion/sync_to_physics

Default

false

Setter

set_sync_to_physics(value)

Getter

is_sync_to_physics_enabled()

如果为true,则物体的运动将与物理帧同步。当通过AnimationPlayer为运动设置动画时,例如在移动平台上,这个功能很有用。请不要move_and_slidemove_and_collide 函数一起使用。


Default

false

Setter

set_axis_lock(value)

Getter

get_axis_lock()

锁定物体的X轴运动。已被废弃的axis_lock_motion_x的别名。


Default

false

Setter

set_axis_lock(value)

Getter

get_axis_lock()

锁定物体的Y轴运动。已被废弃的axis_lock_motion_y的别名。


Default

false

Setter

set_axis_lock(value)

Getter

get_axis_lock()

锁定物体的Z轴运动。已被废弃的axis_lock_motion_z的别名。

方法说明

如果指定的轴 axis 被锁定,则返回 true。请参阅 move_lock_xmove_lock_ymove_lock_z


  • float get_floor_angle ( Vector3 up_direction=Vector3( 0, 1, 0 ) ) const

根据up_direction返回最后一个碰撞点的地板碰撞角度,默认为Vector3.UP。此值始终为正值,并且仅在调用 move_and_slide 后且当 is_on_floor 返回 true 时有效。


  • Vector3 get_floor_normal ( ) const

返回最后一个碰撞点的地板的表面法线。只有在调用move_and_slidemove_and_slide_with_snap后,以及is_on_floor返回true时才有效。


  • Vector3 get_floor_velocity ( ) const

返回最后一个碰撞点地板的线速度。仅在调用 move_and_slidemove_and_slide_with_snapis_on_floor 返回 true 后有效。


返回 KinematicCollision,其中包含有关上次调用 move_and_slide 期间发生的最新碰撞的信息。


返回 KinematicCollision,其中包含有关上次调用 move_and_slidemove_and_slide_with_snap 期间发生的碰撞的信息。由于物体可以在一次调用 move_and_slide 时发生多次碰撞,因此你必须在 0 到 (get_slide_count - 1) 范围内指定碰撞的索引。


  • int get_slide_count ( ) const

返回上次调用 move_and_slidemove_and_slide_with_snap 期间物体发生碰撞和改变方向的次数。


  • bool is_on_ceiling ( ) const

如果在最后一次调用 move_and_slidemove_and_slide_with_snap 时物体与天花板发生碰撞,则返回 true。否则,返回 false


  • bool is_on_floor ( ) const

如果在最后一次调用 move_and_slidemove_and_slide_with_snap 时物体与地板发生碰撞,则返回 true。否则,返回 false


  • bool is_on_wall ( ) const

如果在最后一次调用 move_and_slidemove_and_slide_with_snap 时物体与墙壁发生碰撞,则返回 true。否则,返回 false


沿向量 rel_vec 移动物体。如果发生碰撞,物体会停止。停止或在运动时与另一物体相接触时,返回包含碰撞相关信息的 KinematicCollision

如果 test_onlytrue,则物体不会移动,但会给出可能发生的碰撞信息。


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

沿着向量移动物体。如果这个物体与另一个物体相撞,它将沿着另一个物体滑动,而不是立即停止。如果另一个物体是KinematicBodyRigidBody,它也会被另一个物体的运动所影响。你可以用它来制作移动和旋转的平台,或者让节点推动其他节点。

这个方法应该在Node._physics_process中使用,或者在被Node._physics_process调用的方法中使用,因为它在计算时,自动使用物理步骤的delta值。否则,模拟将以不正确的速度运行。

linear_velocity是速度向量,通常是米/秒。与move_and_collide不同的是,你不应该把它乘以delta--物理引擎会处理应用速度。

up_direction 是向上的方向,用来确定什么是墙,什么是地板或天花板。如果设置为默认值Vector3(0, 0, 0),一切都被认为是墙。

如果stop_on_slopetrue,当你在linear_velocity中包含重力并且物体静止时,物体将不会在斜坡上滑动。

如果物体发生碰撞,它最多会改变方向max_slides次才会停止。

floor_max_angle是一个最大的角度(弧度),在这个角度下,一个斜坡仍然被认为是地板或天花板,而不是墙壁。默认值等于45度。

如果infinite_inertiatrue,物体将能够推动RigidBody节点,但它也不会检测到任何与它们的碰撞。如果false,它将与RigidBody节点像StaticBody一样交互。

返回linear_velocity向量,如果发生滑动碰撞,则旋转和/或缩放。要获得发生碰撞的详细信息,请使用get_slide_collision

当物体接触到一个移动的平台时,平台的速度会自动加入到物体的运动中。如果由于平台的运动而发生碰撞,它将始终是滑动碰撞中的第一个。


  • Vector3 move_and_slide_with_snap ( Vector3 linear_velocity, Vector3 snap, Vector3 up_direction=Vector3( 0, 0, 0 ), bool stop_on_slope=false, int max_slides=4, float floor_max_angle=0.785398, bool infinite_inertia=true )

移动物体,其同时保持与斜坡的连接。类似于move_and_slide

只要snap向量与地面接触,物体就会一直吸附在地面上。这意味着你必须禁用snap来进行跳跃,比如,可以通过将snap设置为(0, 0, 0)或者使用move_and_slide来实现。


根据lock的值,锁定或解锁指定的axis。参阅move_lock_xmove_lock_ymove_lock_z


在不移动物体的情况下检查碰撞情况。假装将节点的位置、缩放和旋转设置为给定的 Transform,然后尝试沿着向量 rel_vec 移动物体。如果物体在沿着完整路径移动过程中会发生碰撞停止,则返回 true

要检测物体接触而产生的碰撞,请使用 move_and_collide