基础物理

Roblox 物理引擎允许开发者在游戏中创建逼真的互动和运动。理解和操控物理组件可以大大提升游戏体验和沉浸感。

组件描述使用案例
BodyForce对部件施加恒定的力。模拟持续推力,例如火箭发动机。
BodyVelocity通过调整施加的力来维持指定的速度。保持车辆或平台以恒定速度移动。
BodyPosition将部件移动到指定位置并保持在那里。创建一个移动到目标位置并保持的部件,例如电梯。
BodyGyro通过扭矩维持部件的方向。稳定部件的旋转,例如保持火箭的直立。
BodyAngularVelocity对部件施加恒定的旋转速度。以恒定速度旋转部件,例如旋转风扇。

基础物理示例

BodyVelocity

该组件通过施加持续的力来维持特定方向上的速度,覆盖部件上作用的其他力。
Image 1
Roblox Studio
local parentPart = script.Parent -- Reference to the parent part

local velocity = Instance.new("BodyVelocity")
velocity.Velocity = Vector3.new(0, 50, 0) -- Move upward 
velocity.MaxForce = Vector3.new(0, 10000, 0) -- Apply force on Y axes
velocity.Parent = parentPart -- Apply the BodyVelocity to the parent part

Velocity

这设置了初始向上的速度。由于重力和摩擦等外力的作用,速度会随着时间的推移而减小。
Image 1
Roblox Studio
local parentPart = script.Parent -- Reference to the parent part

parentPart.Velocity = Vector3.new(0, 50, 0) -- Move upward

BodyForce

该组件在特定方向上施加持续的力,通常用于抵消重力或模拟恒定的加速度。与 BodyVelocity 不同,它不直接控制速度,而是通过施加一致的力来影响运动。
Image 1
Roblox Studio
local parentPart = script.Parent -- Reference to the parent part

local force = Instance.new("BodyForce")
force.Force = Vector3.new(0, 5000, 0) -- Apply upward force of 5000 on the Y-axis
force.Parent = parentPart -- Apply the BodyForce to the parent part

BodyGyro

该组件持续施加力以将部件移动到指定位置并保持在那里,覆盖任何外力。
Image 1
Roblox Studio
local parentPart = script.Parent -- Reference to the parent part

local gyro = Instance.new("BodyGyro")
gyro.CFrame = CFrame.Angles(math.rad(45), math.rad(30), 0) -- Desired rotation
gyro.MaxTorque = Vector3.new(4000, 4000, 4000) -- Maximum torque applied on each axis
gyro.P = 3000 -- Power
gyro.Parent = parentPart -- Apply the BodyGyro to the parent part

BodyPosition

该组件持续施加力以将部件移动到指定位置并保持在那里,抵消任何外力。
Image 1
Roblox Studio
local parentPart = script.Parent -- Reference to the parent part

local position = Instance.new("BodyPosition")
position.Position = Vector3.new(0, 5, 0) -- Target position
position.MaxForce = Vector3.new(4000, 4000, 4000) -- Maximum force applied to reach the target position
position.D = 1000 -- Damping
position.P = 5000 -- Power
position.Parent = parentPart -- Apply the BodyPosition to the parent part

BodyAngularVelocity

该组件持续施加扭矩以维持绕 Y 轴的恒定旋转速度,覆盖任何阻力。
Image 1
Roblox Studio
local parentPart = script.Parent -- Reference to the parent part

local angularVelocity = Instance.new("BodyAngularVelocity")
angularVelocity.AngularVelocity = Vector3.new(0, 2, 0) -- Rotate around Y-axis
angularVelocity.MaxTorque = Vector3.new(0, 10000, 0) -- Maximum torque applied
angularVelocity.Parent = parentPart -- Apply the BodyAngularVelocity to the parent part