エンジンクラス
BasePart
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
概要
プロパティ
方法
イベント
StoppedTouching(otherPart: BasePart):RBXScriptSignal |
Touched(otherPart: BasePart):RBXScriptSignal |
TouchEnded(otherPart: BasePart):RBXScriptSignal |
コードサンプル
パーツのCFrameをスムーズにダンピングする
local TweenService = game:GetService("TweenService")
-- パーツとターゲットを作成
local part = Instance.new("Part", workspace)
part.Size = Vector3.one
part.Anchored = true
part.BrickColor = BrickColor.new("Electric blue")
part.CFrame = CFrame.new(0, 4, 0)
local target = Instance.new("Part", workspace)
target.Name = "Target"
target.Size = Vector3.one * 0.5
target.Anchored = true
target.BrickColor = BrickColor.new("Really red")
target.CFrame = CFrame.new(0, 8, 0)
-- 速度を保存するための変数を設定
local velocity = CFrame.new()
-- スムージング速度を調整
local smoothTime = 0.5
game:GetService("RunService").Stepped:Connect(function(t, dt)
part.CFrame, velocity = TweenService:SmoothDamp(part.CFrame, target.CFrame, velocity, smoothTime, math.huge, dt)
end)APIリファレンス
プロパティ
Anchored
コードサンプル
パートの固定切替
local part = script.Parent
-- パートがクリックされたときに通知を受け取るためにClickDetectorを作成
local cd = Instance.new("ClickDetector", part)
-- この関数は、パートの固定状態に基づいて外観を更新します
local function updateVisuals()
if part.Anchored then
-- パートが固定されているとき...
part.BrickColor = BrickColor.new("明るい赤")
part.Material = Enum.Material.DiamondPlate
else
-- パートが固定されていないとき...
part.BrickColor = BrickColor.new("明るい黄色")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- 固定プロパティを切り替える
part.Anchored = not part.Anchored
-- ブロックの視覚状態を更新
updateVisuals()
end
-- 更新して、クリックのリスニングを開始
updateVisuals()
cd.MouseClick:Connect(onToggle)BackParamA
BackParamB
BackSurfaceInput
BottomParamA
BottomParamB
BottomSurfaceInput
brickColor
CanCollide
コードサンプル
フェードドア
-- 高さのあるパーツ内のスクリプトに貼り付けてください
local part = script.Parent
local OPEN_TIME = 1
-- 現在ドアを開けることができますか?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("黒")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("明るい青")
end
local function onTouch(otherPart)
-- ドアがすでに開いている場合は何もしない
if debounce then
print("D")
return
end
-- Humanoidに触れたか確認
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("ヒューマノイドではありません")
return
end
-- ドアの開閉シーケンスを実行
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()CFrame
コードサンプル
パートのCFrameを設定
local part = script.Parent:WaitForChild("Part")
local otherPart = script.Parent:WaitForChild("OtherPart")
-- パートのCFrameを(0, 0, 0)にリセット(回転なし)。
-- これは時々「恒等」CFrameと呼ばれます
part.CFrame = CFrame.new()
-- 特定の位置(X, Y, Z)に設定
part.CFrame = CFrame.new(0, 25, 10)
-- 上記と同じですが、Vector3を使用
local point = Vector3.new(0, 25, 10)
part.CFrame = CFrame.new(point)
-- パートのCFrameを一つの点に設定し、別の点を見つめる
local lookAtPoint = Vector3.new(0, 20, 15)
part.CFrame = CFrame.lookAt(point, lookAtPoint)
-- パートのCFrameをローカルX軸でpi/2ラジアン回転
part.CFrame = part.CFrame * CFrame.Angles(math.pi / 2, 0, 0)
-- パートのCFrameをローカルY軸で45度回転
part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(45), 0)
-- グローバルZ軸でパートのCFrameを180度回転(順番に注意!)
part.CFrame = CFrame.Angles(0, 0, math.pi) * part.CFrame -- Piラジアンは180度に等しい
-- 二つのCFrameを構成するのは*(乗算演算子)を使用します
part.CFrame = CFrame.new(2, 3, 4) * CFrame.new(4, 5, 6) --> CFrame.new(6, 8, 10)に等しい
-- 代数の乗算とは異なり、CFrameの合成はコミュニケイティブではありません:a * bは必ずしもb * aではありません!
-- *を秩序のある一連のアクションとして想像してください。例えば、以下の行は異なるCFrameを生成します:
-- 1) パートをX方向に5ユニットスライド。
-- 2) パートをY軸周りに45度回転。
part.CFrame = CFrame.new(5, 0, 0) * CFrame.Angles(0, math.rad(45), 0)
-- 1) パートをY軸周りに45度回転。
-- 2) パートをX方向に5ユニットスライド。
part.CFrame = CFrame.Angles(0, math.rad(45), 0) * CFrame.new(5, 0, 0)
-- 「CFrame除算」はありませんが、単に「逆の操作を行う」だけです。
part.CFrame = CFrame.new(4, 5, 6) * CFrame.new(4, 5, 6):Inverse() --> CFrame.new(0, 0, 0)に等しい
part.CFrame = CFrame.Angles(0, 0, math.pi) * CFrame.Angles(0, 0, math.pi):Inverse() --> CFrame.Angles(0, 0, 0)に等しい
-- 他のパートに対して相対的にパートを配置(この場合、私たちのパートをotherPartの上に置く)
part.CFrame = otherPart.CFrame * CFrame.new(0, part.Size.Y / 2 + otherPart.Size.Y / 2, 0)CollisionGroup
コードサンプル
PhysicsService:RegisterCollisionGroup
local PhysicsService = game:GetService("PhysicsService")
local collisionGroupBall = "CollisionGroupBall"
local collisionGroupDoor = "CollisionGroupDoor"
-- 衝突グループを登録します
PhysicsService:RegisterCollisionGroup(collisionGroupBall)
PhysicsService:RegisterCollisionGroup(collisionGroupDoor)
-- 部品を衝突グループに割り当てます
script.Parent.BallPart.CollisionGroup = collisionGroupBall
script.Parent.DoorPart.CollisionGroup = collisionGroupDoor
-- グループを互いに非衝突に設定し、結果を確認します
PhysicsService:CollisionGroupSetCollidable(collisionGroupBall, collisionGroupDoor, false)
print(PhysicsService:CollisionGroupsAreCollidable(collisionGroupBall, collisionGroupDoor)) --> falseCollisionGroupId
Color
コードサンプル
キャラクターの健康状態に基づく体の色
-- StarterCharacterScripts内のスクリプトに貼り付けてください
-- その後、ゲームをプレイし、キャラクターの健康状態をいじってみてください
local char = script.Parent
local human = char.Humanoid
local colorHealthy = Color3.new(0.4, 1, 0.2)
local colorUnhealthy = Color3.new(1, 0.4, 0.2)
local function setColor(color)
for _, child in pairs(char:GetChildren()) do
if child:IsA("BasePart") then
child.Color = color
while child:FindFirstChildOfClass("Decal") do
child:FindFirstChildOfClass("Decal"):Destroy()
end
elseif child:IsA("Accessory") then
child.Handle.Color = color
local mesh = child.Handle:FindFirstChildOfClass("SpecialMesh")
if mesh then
mesh.TextureId = ""
end
elseif child:IsA("Shirt") or child:IsA("Pants") then
child:Destroy()
end
end
end
local function update()
local percentage = human.Health / human.MaxHealth
-- 健康の割合に基づいて補間することで、色を作成
-- 色は colorHealthy (100%) ----- > colorUnhealthy (0%) に変わります
local color = Color3.new(
colorHealthy.R * percentage + colorUnhealthy.r * (1 - percentage),
colorHealthy.G * percentage + colorUnhealthy.g * (1 - percentage),
colorHealthy.B * percentage + colorUnhealthy.b * (1 - percentage)
)
setColor(color)
end
update()
human.HealthChanged:Connect(update)CustomPhysicalProperties
コードサンプル
カスタム物理プロパティの設定
local part = script.Parent
-- これにより、部品が軽くてバウンドします!
local DENSITY = 0.3
local FRICTION = 0.1
local ELASTICITY = 1
local FRICTION_WEIGHT = 1
local ELASTICITY_WEIGHT = 1
local physProperties = PhysicalProperties.new(DENSITY, FRICTION, ELASTICITY, FRICTION_WEIGHT, ELASTICITY_WEIGHT)
part.CustomPhysicalProperties = physPropertiesElasticity
Friction
FrontParamA
FrontParamB
FrontSurfaceInput
LeftParamA
LeftParamB
LeftSurfaceInput
Locked
コードサンプル
再帰的アンロック
-- アンロックしたいモデル内のスクリプトに貼り付けてください
local model = script.Parent
-- この関数はモデルの階層を再帰的に辿って
-- 出会ったすべてのパーツをアンロックします。
local function recursiveUnlock(object)
if object:IsA("BasePart") then
object.Locked = false
end
-- オブジェクトの子供に同じ関数を呼び出します
-- オブジェクトに子供がいない場合、再帰プロセスは停止します
for _, child in pairs(object:GetChildren()) do
recursiveUnlock(child)
end
end
recursiveUnlock(model)Orientation
コードサンプル
パーツスピナー
local part = script.Parent
local INCREMENT = 360 / 20
-- パーツを継続的に回転させる
while true do
for degrees = 0, 360, INCREMENT do
-- Y軸の回転のみを設定する
part.Rotation = Vector3.new(0, degrees, 0)
-- これを行うより良い方法はCFrameを設定することです
--part.CFrame = CFrame.new(part.Position) * CFrame.Angles(0, math.rad(degrees), 0)
task.wait()
end
endPivotOffset
コードサンプル
ピボットをリセット
local function resetPivot(model)
local boundsCFrame = model:GetBoundingBox()
if model.PrimaryPart then
model.PrimaryPart.PivotOffset = model.PrimaryPart.CFrame:ToObjectSpace(boundsCFrame)
else
model.WorldPivot = boundsCFrame
end
end
resetPivot(script.Parent)時計の針
local function createHand(length, width, yOffset)
local part = Instance.new("Part")
part.Size = Vector3.new(width, 0.1, length)
part.Material = Enum.Material.Neon
part.PivotOffset = CFrame.new(0, -(yOffset + 0.1), length / 2)
part.Anchored = true
part.Parent = workspace
return part
end
local function positionHand(hand, fraction)
hand:PivotTo(CFrame.fromEulerAnglesXYZ(0, -fraction * 2 * math.pi, 0))
end
-- ダイヤルを作成
for i = 0, 11 do
local dialPart = Instance.new("Part")
dialPart.Size = Vector3.new(0.2, 0.2, 1)
dialPart.TopSurface = Enum.SurfaceType.Smooth
if i == 0 then
dialPart.Size = Vector3.new(0.2, 0.2, 2)
dialPart.Color = Color3.new(1, 0, 0)
end
dialPart.PivotOffset = CFrame.new(0, -0.1, 10.5)
dialPart.Anchored = true
dialPart:PivotTo(CFrame.fromEulerAnglesXYZ(0, (i / 12) * 2 * math.pi, 0))
dialPart.Parent = workspace
end
-- 針を作成
local hourHand = createHand(7, 1, 0)
local minuteHand = createHand(10, 0.6, 0.1)
local secondHand = createHand(11, 0.2, 0.2)
-- 時計を動かす
while true do
local components = os.date("*t")
positionHand(hourHand, (components.hour + components.min / 60) / 12)
positionHand(minuteHand, (components.min + components.sec / 60) / 60)
positionHand(secondHand, components.sec / 60)
task.wait()
endResizeableFaces
コードサンプル
リサイズハンドル
-- このスクリプトをさまざまな種類のBasePartに置いてください。
-- Part、TrussPart、WedgePart、CornerWedgePartなど。
local part = script.Parent
-- このパートのためにハンドルオブジェクトを作成します
local handles = Instance.new("Handles")
handles.Adornee = part
handles.Parent = part
-- このハンドルに適用可能な面を手動で指定します
handles.Faces = Faces.new(Enum.NormalId.Top, Enum.NormalId.Front, Enum.NormalId.Left)
-- あるいは、パートがリサイズ可能な面を使用します。
-- パートが長さ2のサイズの次元が2つのみのTrussPartである場合
-- ResizeableFacesは2つの有効な面しか持ちません。
-- 他のパーツの場合は、すべての面が有効になります。
handles.Faces = part.ResizeableFacesRightParamA
RightParamB
RightSurfaceInput
RotVelocity
Size
コードサンプル
ピラミッドビルダー
local TOWER_BASE_SIZE = 30
local position = Vector3.new(50, 50, 50)
local hue = math.random()
local color0 = Color3.fromHSV(hue, 1, 1)
local color1 = Color3.fromHSV((hue + 0.35) % 1, 1, 1)
local model = Instance.new("Model")
model.Name = "Tower"
for i = TOWER_BASE_SIZE, 1, -2 do
local part = Instance.new("Part")
part.Size = Vector3.new(i, 2, i)
part.Position = position
part.Anchored = true
part.Parent = model
-- color0 と color1 の間でトゥイーンします
local perc = i / TOWER_BASE_SIZE
part.Color = Color3.new(
color0.R * perc + color1.R * (1 - perc),
color0.G * perc + color1.G * (1 - perc),
color0.B * perc + color1.B * (1 - perc)
)
position = position + Vector3.new(0, part.Size.Y, 0)
end
model.Parent = workspaceSpecificGravity
TopParamA
TopParamB
TopSurfaceInput
Transparency
コードサンプル
フェードドア
-- 高さのあるパーツ内のスクリプトに貼り付けてください
local part = script.Parent
local OPEN_TIME = 1
-- 現在ドアを開けることができますか?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("黒")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("明るい青")
end
local function onTouch(otherPart)
-- ドアがすでに開いている場合は何もしない
if debounce then
print("D")
return
end
-- Humanoidに触れたか確認
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("ヒューマノイドではありません")
return
end
-- ドアの開閉シーケンスを実行
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()Velocity
方法
AngularAccelerationToTorque
ApplyImpulseAtPosition
BreakJoints
breakJoints
CanSetNetworkOwnership
戻り値
コードサンプル
パーツのネットワーク所有権が設定可能かどうかを確認する
local part = workspace:FindFirstChild("Part")
if part and part:IsA("BasePart") then
local canSet, errorReason = part:CanSetNetworkOwnership()
if canSet then
print(part:GetFullName() .. "のネットワーク所有権は変更可能です!")
else
warn("ネットワーク所有権を変更できません: " .. part:GetFullName() .. " because: " .. errorReason)
end
endGetClosestPointOnSurface
GetConnectedParts
GetJoints
BasePart:GetJoints():Instances
戻り値
Instances
GetMass
getMass
GetNoCollisionConstraints
BasePart:GetNoCollisionConstraints():Instances
戻り値
Instances
GetRenderCFrame
GetRootPart
GetTouchingParts
BasePart:GetTouchingParts():Instances
戻り値
Instances
GetVelocityAtPosition
IntersectAsync
BasePart:IntersectAsync(
パラメータ
parts:Instances |
| 既定値: "Default" |
| 既定値: "Automatic" |
戻り値
MakeJoints
makeJoints
Resize
パラメータ
戻り値
resize
SetNetworkOwner
SetNetworkOwnershipAuto
BasePart:SetNetworkOwnershipAuto():()
戻り値
()
SubtractAsync
BasePart:SubtractAsync(
パラメータ
parts:Instances |
| 既定値: "Default" |
| 既定値: "Automatic" |
戻り値
コードサンプル
BasePart:SubtractAsync()
local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- 減算操作を実行
local success, newSubtract = pcall(function()
return mainPart:SubtractAsync(otherParts)
end)
-- 操作が成功した場合は、同じ場所に配置してワークスペースに親を設定します
if success and newSubtract then
newSubtract.Position = mainPart.Position
newSubtract.Parent = Workspace
end
-- 操作後も無傷の元のパーツを破壊します
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
endTorqueToAngularAcceleration
UnionAsync
BasePart:UnionAsync(
パラメータ
parts:Instances |
| 既定値: "Default" |
| 既定値: "Automatic" |
戻り値
コードサンプル
BasePart:UnionAsync()
local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- ユニオン操作を実行します
local success, newUnion = pcall(function()
return mainPart:UnionAsync(otherParts)
end)
-- 操作が成功したら、同じ位置に配置し、ワークスペースに親を設定します
if success and newUnion then
newUnion.Position = mainPart.Position
newUnion.Parent = Workspace
end
-- 操作後も無傷の元のパーツを破壊します
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
endイベント
LocalSimulationTouched
OutfitChanged
StoppedTouching
Touched
パラメータ
コードサンプル
接触している部品の数
local part = script.Parent
local billboardGui = Instance.new("BillboardGui")
billboardGui.Size = UDim2.new(0, 200, 0, 50)
billboardGui.Adornee = part
billboardGui.AlwaysOnTop = true
billboardGui.Parent = part
local tl = Instance.new("TextLabel")
tl.Size = UDim2.new(1, 0, 1, 0)
tl.BackgroundTransparency = 1
tl.Parent = billboardGui
local numTouchingParts = 0
local function onTouch(otherPart)
print("接触開始: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("接触終了: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)モデルが触れた
local model = script.Parent
local function onTouched(otherPart)
-- モデルのインスタンスが自身と接触するのを無視する
if otherPart:IsDescendantOf(model) then
return
end
print(model.Name .. "が" .. otherPart.Name .. "と衝突しました")
end
for _, child in pairs(model:GetChildren()) do
if child:IsA("BasePart") then
child.Touched:Connect(onTouched)
end
endTouchEnded
パラメータ
コードサンプル
接触している部品の数
local part = script.Parent
local billboardGui = Instance.new("BillboardGui")
billboardGui.Size = UDim2.new(0, 200, 0, 50)
billboardGui.Adornee = part
billboardGui.AlwaysOnTop = true
billboardGui.Parent = part
local tl = Instance.new("TextLabel")
tl.Size = UDim2.new(1, 0, 1, 0)
tl.BackgroundTransparency = 1
tl.Parent = billboardGui
local numTouchingParts = 0
local function onTouch(otherPart)
print("接触開始: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("接触終了: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)