Empty
Empty
Empty
Roblox 物理引擎允許開發者在遊戲中創造逼真的互動和運動。理解和操控物理元件可以大大提升遊戲體驗和沉浸感。
元件 | 描述 | 使用案例 |
---|---|---|
BodyForce | 對部件施加恆定的力量。 | 模擬持續推力,例如火箭發動機。 |
BodyVelocity | 通過調整施加的力量來維持指定的速度。 | 保持車輛或平台以恆定速度移動。 |
BodyPosition | 將部件移動到指定位置並保持在那裡。 | 建立一個移動到目標位置並保持的部件,例如電梯。 |
BodyGyro | 通過扭矩維持部件的方向。 | 穩定部件的旋轉,例如保持火箭的直立。 |
BodyAngularVelocity | 對部件施加恆定的旋轉速度。 | 以恆定速度旋轉部件,例如旋轉風扇。 |
基本物理範例
BodyVelocity
該元件通過施加持續的力量來維持特定方向上的速度,覆蓋部件上作用的其他力量。
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
速度
這設置了初始向上的速度。由於重力和摩擦等外力的作用,速度會隨著時間的推移而減少。
Roblox Studio
local parentPart = script.Parent -- Reference to the parent part
parentPart.Velocity = Vector3.new(0, 50, 0) -- Move upward
BodyForce
該元件在特定方向上施加持續的力量,通常用於抵消重力或模擬恆定的加速度。與 BodyVelocity 不同,它不直接控制速度,而是通過施加一致的力量來影響運動。
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
該元件持續施加力量以將部件移動到指定位置並保持在那裡,覆蓋任何外力。
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
該元件持續施加力量以將部件移動到指定位置並保持在那裡,抵消任何外力。
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 軸的恆定旋轉速度,覆蓋任何阻力。
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