BasePart
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
BasePart เป็นคลาสระดับสูงสำหรับวัตถุในโลกที่แสดงและจำลองทางกายภาพในขณะที่อยู่ใน Workspaceมีการใช้งานหลายอย่างของ BasePart , ที่พบบ่อยที่สุดคือ Part และ MeshPartอื่นๆ รวมถึง WedgePart , SpawnLocation และวัตถุโซลเดียว Terrainโดยทั่วไปเมื่อเอกสารอ้างถึง "ส่วน" ส่วนใหญ่ BasePart การดำเนินการจะทำงานและไม่เพียงแค่ Part
สำหรับข้อมูลเกี่ยวกับวิธีที่ BaseParts จะถูกจัดเป็นกลุ่มเป็นร่างแข็งจำลองดูได้ที่ ชิ้นส่วน
มีวัตถุที่แตกต่างกันมากมายที่โต้ตอบกับ BasePart (นอกเหนือจาก Terrain ) รวมถึง:
- A Decal ใช้เทกเจอร์ภาพขยายบนใบหน้าของ BasePart ในขณะที่ A Texture ใช้เทกเจอร์ภาพแบบกระเบื้องบนใบหน้าดู เทกเจอร์และภาพวาด .
- Attachments สามารถเพิ่มไปยัง BasePart เพื่อระบุ CFrames เกี่ยวกับส่วนสิ่งเหล่านี้มักใช้โดยวัตถุทางกายภาพ Constraint ตามที่อธิบายไว้ใน ข้อจํากัดทางกล และ ข้อจํากัดของตัวขับเคลื่อน
- ParticleEmitter วัตถุปล่อยอนุภาคอย่างสม่ำเสมอในปริมาณของ BasePart ที่พวกเขาเป็นพ่อแม่ ดู ผู้ปล่อยอนุภาค
สรุป
คุณสมบัติ
กำหนดว่าชิ้นส่วนเป็นอนันต์ตามฟิสิกส์หรือไม่
ความเร็วในแนวมุมของการประกอบชิ้นส่วน
ศูนย์กลางของมวลของการประกอบชิ้นส่วนในพื้นที่โลก
ความเร็วเชิงเส้นของการประกอบชิ้นส่วน
มวลรวมของการประกอบชิ้นส่วน
การอ้างอิงถึงส่วนรากของการประกอบ
กำหนดประเภทพื้นผิวสำหรับด้านหลังของชิ้นส่วน (+Z ทิศทาง)
กำหนดประเภทพื้นผิวสำหรับด้านล่างของชิ้นส่วน (-Y ทิศทาง)
กำหนดสีของชิ้นส่วน
กำหนดตำแหน่งและทิศทางของ BasePart ในโลก
กำหนดว่าชิ้นส่วนจะชนกับชิ้นส่วนอื่นหรือไม่
กำหนดว่าชิ้นส่วนจะถูกพิจารณาในระหว่างการดำเนินการค้นหาในพื้นที่หรือไม่
กำหนดว่าจะมีการเรียกใช้อีเวนต์ Touched และ TouchEnded บนส่วนหรือไม่
กำหนดว่าส่วนใดมีเงาหรือไม่
อธิบายตำแหน่งของโลกที่ศูนย์กลางของมวลของชิ้นส่วนอยู่
อธิบายชื่อกลุ่มการชนกันของชิ้นส่วน
กำหนดสีของชิ้นส่วน
บ่งชี้คุณสมบัติทางกายภาพปัจจุบันของชิ้นส่วน
กำหนดคุณสมบัติทางกายภาพหลายอย่างของชิ้นส่วน
ใช้เพื่อเปิดใช้งานหรือปิดใช้งานแรงอากาศในชิ้นส่วนและชิ้นส่วนประกอบ
ขนาดทางกายภาพที่แท้จริงของ BasePart ตามที่เครื่องกลศาสตร์กำหนด
กำหนดประเภทพื้นผิวสำหรับด้านหน้าของชิ้นส่วน (-Z ทิศทาง)
กำหนดประเภทพื้นผิวสำหรับด้านซ้ายของชิ้นส่วน (-X ทิศทาง)
กำหนดตัวคูณสำหรับ BasePart.Transparency ที่มองเห็นได้เฉพาะกับไคลเอนต์ท้องถิ่นเท่านั้น
กำหนดว่าส่วนใดสามารถเลือกได้ใน Studio
อธิบายมวลของชิ้นส่วน ผลิตภัณฑ์ของความหนาแน่นและปริมาณ
กำหนดว่าชิ้นส่วนมีส่วนร่วมในมวลหรือแรงเฉื่อยของร่างกายแข็งหรือไม่
กำหนดเทกเจอร์และคุณสมบัติทางกายภาพเริ่มต้นของชิ้นส่วน
ชื่อของ MaterialVariant .
อธิบายการหมุนของชิ้นส่วนในโลก
ระบุออฟเซ็ตของจุดศูนย์กลางของชิ้นส่วนจาก CFrame ของมัน
อธิบายตำแหน่งของชิ้นส่วนในโลก
เวลาตั้งแต่อัปเดตฟิสิกส์ครั้งล่าสุด
กำหนดจํานวนส่วนที่สะท้อนกล่องท้องฟ้าได้
อธิบายการเปลี่ยนแปลงขนาดเล็กที่สุดที่อนุญาตโดยวิธีการปรับขนาด
อธิบายใบหน้าที่ส่วนสามารถปรับขนาดได้
กำหนดประเภทพื้นผิวสำหรับด้านขวาของชิ้นส่วน (+X ทิศทาง)
กฎหลักในการกำหนดส่วนรากของการประกอบ
การหมุนของชิ้นส่วนในองศาสำหรับสามแกน
กำหนดขนาดของชิ้นส่วน (ความยาว ความกว้าง ความสูง)
กำหนดประเภทพื้นผิวสำหรับด้านบนของชิ้นส่วน (+Y ทิศทาง)
กำหนดจำนวนส่วนที่สามารถมองเห็นได้ผ่าน (ตรงกันข้ามของความทึบของส่วน)
วิธีการ
ใช้แรงกระตุกแบบมุมกับการประกอบ
ใช้แรงกระตุ้นกับการประกอบที่การประกอบ center of mass .
ใช้แรงกระตุ้นกับการประกอบในตำแหน่งที่กำหนด
ส่งคืนว่าชิ้นส่วนสามารถชนกันได้หรือไม่
ตรวจสอบว่าคุณสามารถกำหนดสิทธิ์เจ้าของเครือข่ายส่วนได้หรือไม่
คืนตารางชิ้นส่วนที่เชื่อมโยงกับวัตถุโดยใช้ข้อต่อแข็งใดๆ
คืนข้อต่อหรือข้อจำกัดทั้งหมดที่เชื่อมโยงกับส่วนนี้
ส่งค่าของคุณสมบัติ Mass กลับ
ส่งคืนผู้เล่นปัจจุบันที่เป็นเจ้าของเครือข่ายส่วนนี้หรือ nil ในกรณีของเซิร์ฟเวอร์
ส่งคืนค่าจริงหากเครื่องเกมตัดสินเจ้าของเครือข่ายสำหรับส่วนนี้โดยอัตโนมัติ
คืนส่วนฐานของการประกอบชิ้นส่วน
คืนตารางของชิ้นส่วนทั้งหมด BasePart.CanCollide ที่ถูกตัดกับชิ้นส่วนนี้
คืนความเร็วเชิงเส้นของการประกอบชิ้นส่วนในตำแหน่งที่กำหนดเมื่อเทียบกับชิ้นส่วนนี้
ส่งคืนจริงหากวัตถุเชื่อมต่อกับส่วนที่จะถือไว้ในตําแหน่ง (เช่นส่วน Anchored ) มิฉะนั้นจะส่งคืนเป็นเท็จ
เปลี่ยนขนาดของวัตถุเช่นเดียวกับการใช้เครื่องมือปรับขนาดของ Studio
ตั้งผู้เล่นที่กำหนดเป็นเจ้าของเครือข่ายสำหรับส่วนที่เชื่อมต่อทั้งหมดและส่วนนี้
ให้เครื่องเกมตัดสินได้อย่างไดนามิกว่าใครจะจัดการกับฟิสิกส์ของชิ้นส่วน (หนึ่งในไคลเอนต์หรือเซิร์ฟเวอร์)
- IntersectAsync(parts : Instances,collisionfidelity : Enum.CollisionFidelity,renderFidelity : Enum.RenderFidelity):Instance
สร้างใหม่ IntersectOperation จากเรขาคณิตที่ซ้อนทับของชิ้นส่วนและชิ้นส่วนอื่นๆ ในช่วงที่กำหนด
- SubtractAsync(parts : Instances,collisionfidelity : Enum.CollisionFidelity,renderFidelity : Enum.RenderFidelity):Instance
สร้างใหม่ UnionOperation จากส่วนโดยลบเอกภาพที่ใช้โดยชิ้นส่วนในช่องที่กำหนด
- UnionAsync(parts : Instances,collisionfidelity : Enum.CollisionFidelity,renderFidelity : Enum.RenderFidelity):Instance
สร้างใหม่ UnionOperation จากส่วน, บวกกับรูปร่างที่ใช้โดยชิ้นส่วนในช่วงที่กำหนด
รับจุดศูนย์กลางของ PVInstance .
เปลี่ยน PVInstance พร้อมกับบรรดาลูกหลานทั้งหมดของมัน PVInstances ทำให้จุดศูนย์กลางอยู่ที่ตําแหน่งที่ระบุแล้ว CFrame
อีเวนต์
ไฟเมื่อชิ้นหยุดสัมผัสอีกชิ้นเนื่องจากการเคลื่อนไหวทางกายภาพ
ไฟเมื่อชิ้นส่วนสัมผัสกับชิ้นส่วนอื่นเนื่องจากการเคลื่อนไหวทางกายภาพ
คุณสมบัติ
Anchored
คุณสมบัติ พิน กำหนดว่าชิ้นส่วนจะไม่สามารถย้ายได้โดยฟิสิกส์เมื่อเปิดใช้งานแล้ว ส่วนจะไม่เปลี่ยนตำแหน่งเนื่องจากแรงโน้มถ่วง การชนกันของส่วนอื่น ๆ ซ้อนทับส่วนอื่น ๆ หรือสาเหตุทางฟิสิกส์อื่น ๆผลที่ตามมาคือสองส่วนที่ถูกยึดจะไม่เคยยิงเหตุการณ์ BasePart.Touched ให้กับอีกฝ่ายหนึ่ง
ส่วนที่ถูกยึดอาจยังคงเคลื่อนไหวได้โดยการเปลี่ยน CFrame หรือ Position และยังอาจมี AssemblyLinearVelocity และ AssemblyAngularVelocity ที่ไม่เป็นศูนย์
สุดท้ายหากส่วนที่ไม่ได้ติดตั้งถูกเชื่อมโยงกับส่วนที่ติดตั้งผ่านวัตถุเช่น Weld ก็จะกระทำตามตัวเชื่อมต่อเช่นกันหากข้อต่อดังกล่าวแตก ส่วนอาจได้รับผลกระทบจากฟิสิกส์อีกครั้งดู ชิ้นส่วน สำหรับรายละเอียดเพิ่มเติม
ไม่สามารถตั้งค่าเจ้าของเครือข่ายในส่วนที่ติดตั้งได้หากสถานะที่ติดตั้งของส่วนเปลี่ยนแปลงบนเซิร์ฟเวอร์ เจ้าของเครือข่ายของส่วนนั้นจะได้รับผลกระทบ
ตัวอย่างโค้ด
This code sample will allow a part to be clicked to toggle its anchored property. When toggled, the visual appearance of the part is updated (red means anchored, yellow means free).
local part = script.Parent
-- Create a ClickDetector so we can tell when the part is clicked
local cd = Instance.new("ClickDetector", part)
-- This function updates how the part looks based on its Anchored state
local function updateVisuals()
if part.Anchored then
-- When the part is anchored...
part.BrickColor = BrickColor.new("Bright red")
part.Material = Enum.Material.DiamondPlate
else
-- When the part is unanchored...
part.BrickColor = BrickColor.new("Bright yellow")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- Toggle the anchored property
part.Anchored = not part.Anchored
-- Update visual state of the brick
updateVisuals()
end
-- Update, then start listening for clicks
updateVisuals()
cd.MouseClick:Connect(onToggle)
AssemblyAngularVelocity
เวกเตอร์ความเร็วมุมของการประกอบชิ้นส่วนนี้ เป็นอัตราการเปลี่ยนแปลงของทิศทางในรัศมีต่อวินาที
ความเร็วแบบมุมเป็นเช่นเดียวกันที่ทุกจุดของการประกอบ
การตั้งค่าความเร็วโดยตรงอาจนำไปสู่การเคลื่อนไหวที่ไม่สมจริงการใช้ข้อจํากัด Torque หรือ AngularVelocity จะได้รับการยอมรับ หรือใช้ BasePart:ApplyAngularImpulse() หากต้องการการเปลี่ยนแปลงความเร็วทันที
หากส่วนเป็นเจ้าของ โดยเซิร์ฟเวอร์ คุณสมบัตินี้ต้องเปลี่ยนจากเซิร์ฟเวอร์ (ไม่ใช่จาก หรือ ที่ตั้งค่าไว้เป็น )หากส่วนเป็นของลูกค้าผ่านการเป็นเจ้าของอัตโนมัติ คุณสมบัตินี้สามารถเปลี่ยนได้จากสคริปต์ลูกค้าหรือสคริปต์เซิร์ฟเวอร์; การเปลี่ยนแปลงจากสคริปต์ลูกค้าสำหรับส่วนที่เป็นเจ้าของของเซิร์ฟเวอร์จะไม่มีผล
AssemblyCenterOfMass
ตำแหน่งที่คำนวณผ่าน mass และ position ของชิ้นส่วนทั้งหมดในการประกอบ
หากการประกอบมีส่วนที่ติดตั้งอยู่ ศูนย์กลางของมวลของส่วนนั้นจะเป็นศูนย์กลางของมวลของการประกอบ และการประกอบจะมีมวลไม่มีที่สิ้นสุด
รู้จุดศูนย์กลางของมวลช่วยให้การประกอบรักษาความเสถียรได้แรงที่ใช้กับศูนย์กลางของมวลจะไม่ทําให้เกิดการเร่งแบบมุม เฉพาะแบบเชิงเส้นเท่านั้นการประกอบที่มีศูนย์กลางมวลต่ำจะมีเวลาที่ดีกว่าในการยืนตัวตรงภายใต้ผลกระทบของแรงโน้มถ่วง
AssemblyLinearVelocity
เวกเตอร์ความเร็วเชิงเส้นของการประกอบชิ้นส่วนนี้ เป็นอัตราการเปลี่ยนแปลงในตําแหน่งของการประกอบ center of mass ในสตัดต่อวินาที
หากต้องการทราบความเร็วที่จุดอื่นนอกเหนือจากศูนย์กลางของมวลของการประกอบใช้ BasePart:GetVelocityAtPosition()
การตั้งค่าความเร็วโดยตรงอาจนำไปสู่การเคลื่อนไหวที่ไม่สมจริงการใช้ข้อจํากัด VectorForce จะได้รับการยอมรับ หรือใช้ BasePart:ApplyImpulse() หากต้องการการเปลี่ยนแปลงความเร็วทันที
หากส่วนเป็นเจ้าของ โดยเซิร์ฟเวอร์ คุณสมบัตินี้ต้องเปลี่ยนจากเซิร์ฟเวอร์ (ไม่ใช่จาก หรือ ที่ตั้งค่าไว้เป็น )หากส่วนเป็นของลูกค้าผ่านการเป็นเจ้าของอัตโนมัติ คุณสมบัตินี้สามารถเปลี่ยนได้จากสคริปต์ลูกค้าหรือสคริปต์เซิร์ฟเวอร์; การเปลี่ยนแปลงจากสคริปต์ลูกค้าสำหรับส่วนที่เป็นเจ้าของของเซิร์ฟเวอร์จะไม่มีผล
AssemblyMass
ผลบวกของมวลของทั้งหมด parts ในการประกอบส่วนนี้ชิ้นส่วนที่มี Massless และไม่ใช่ส่วนรากของการประกอบจะไม่มีส่วนร่วมใน AssemblyMass
หากชิ้นส่วนที่ยึดถืออยู่อยู่ในการประกอบ มวลของชิ้นส่วนจะถือว่าเป็นอนันต์ข้อจํากัดและการโต้ตอบทางกายภาพอื่น ๆ ระหว่างชิ้นส่วนที่ไม่ได้ติดตั้งที่มีความแตกต่างขนาดใหญ่อาจทําให้เกิดความไม่เสถียรได้
AssemblyRootPart
คุณสมบัตินี้บ่งบอกถึงการเลือก BasePart โดยอัตโนมัติเพื่อแทนที่ส่วนรากของการรวบรวมเป็นส่วนเดียวกันที่จะถูกส่งคืนเมื่อนักพัฒนาเรียก GetRootPart()
ส่วนรากสามารถเปลี่ยนได้โดยการเปลี่ยน RootPriority ของชิ้นส่วนในการประกอบ
ชิ้นส่วนที่ทั้งหมดแชร์ส่วนประกอบรากเดียวกันเป็นส่วนประกอบเดียวกัน
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับส่วนราก ดู กลุ่มส่วน
AudioCanCollide
BackSurface
คุณสมบัติ BackSurface กำหนดประเภทของพื้นผิวที่ใช้สำหรับทิศทาง +Z ของชิ้นส่วนเมื่อใบหน้าของส่วนสองส่วนถูกวางไว้ข้างๆ กัน พวกเขาอาจสร้างช่องว่างระหว่างพวกเขาหากตั้งค่าเป็นมอเตอร์ BasePart.BackSurfaceInput จะกำหนดวิธีที่ข้อต่อมอเตอร์ควรทำงาน
ประเภทพื้นผิวส่วนใหญ่จะเรนเดอร์เทกเจอร์บนใบหน้าส่วนหาก BasePart.Material ถูกตั้งค่าเป็นพลาสติกบางประเภทพื้นผิว - บานพับ มอเตอร์ และมอเตอร์ก้าว - จะแสดงการตกแต่ง 3D แทนหากคุณสมบัตินี้ถูกเลือกในหน้าต่างคุณสมบัติจะถูกเน้นในโลกเกมที่คล้ายกับของ SurfaceSelection
ตัวอย่างโค้ด
This code sample shows what each SurfaceType looks like on a part. In addition, it creates a BillboardGui label on the part with a TextLabel that reflects the name of the current SurfaceType.
local demoPart = script.Parent
-- Create a billboard gui to display what the current surface type is
local billboard = Instance.new("BillboardGui")
billboard.AlwaysOnTop = true
billboard.Size = UDim2.new(0, 200, 0, 50)
billboard.Adornee = demoPart
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(0, 200, 0, 50)
textLabel.BackgroundTransparency = 1
textLabel.TextStrokeTransparency = 0
textLabel.TextColor3 = Color3.new(1, 1, 1) -- White
textLabel.Parent = billboard
billboard.Parent = demoPart
local function setAllSurfaces(part, surfaceType)
part.TopSurface = surfaceType
part.BottomSurface = surfaceType
part.LeftSurface = surfaceType
part.RightSurface = surfaceType
part.FrontSurface = surfaceType
part.BackSurface = surfaceType
end
while true do
-- Iterate through the different SurfaceTypes
for _, enum in pairs(Enum.SurfaceType:GetEnumItems()) do
textLabel.Text = enum.Name
setAllSurfaces(demoPart, enum)
task.wait(1)
end
end
BottomSurface
คุณสมบัติพื้นผิวด้านล่างกำหนดประเภทพื้นผิวที่ใช้สำหรับทิศทาง -Y ของชิ้นส่วนเมื่อใบหน้าของส่วนสองส่วนถูกวางไว้ข้างๆ กัน พวกเขาอาจสร้างช่องว่างระหว่างพวกเขาหากตั้งค่าเป็นมอเตอร์ BasePart.BottomSurfaceInput จะกำหนดวิธีที่ข้อต่อมอเตอร์ควรทำงาน
ประเภทพื้นผิวส่วนใหญ่จะเรนเดอร์เทกเจอร์บนใบหน้าส่วนหาก BasePart.Material ถูกตั้งค่าเป็นพลาสติกบางประเภทพื้นผิว - บานพับ มอเตอร์ และมอเตอร์ก้าว - จะแสดงการตกแต่ง 3D แทนหากคุณสมบัตินี้ถูกเลือกในหน้าต่างคุณสมบัติจะถูกเน้นในโลกเกมที่คล้ายกับของ SurfaceSelection
ตัวอย่างโค้ด
This code sample shows what each SurfaceType looks like on a part. In addition, it creates a BillboardGui label on the part with a TextLabel that reflects the name of the current SurfaceType.
local demoPart = script.Parent
-- Create a billboard gui to display what the current surface type is
local billboard = Instance.new("BillboardGui")
billboard.AlwaysOnTop = true
billboard.Size = UDim2.new(0, 200, 0, 50)
billboard.Adornee = demoPart
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(0, 200, 0, 50)
textLabel.BackgroundTransparency = 1
textLabel.TextStrokeTransparency = 0
textLabel.TextColor3 = Color3.new(1, 1, 1) -- White
textLabel.Parent = billboard
billboard.Parent = demoPart
local function setAllSurfaces(part, surfaceType)
part.TopSurface = surfaceType
part.BottomSurface = surfaceType
part.LeftSurface = surfaceType
part.RightSurface = surfaceType
part.FrontSurface = surfaceType
part.BackSurface = surfaceType
end
while true do
-- Iterate through the different SurfaceTypes
for _, enum in pairs(Enum.SurfaceType:GetEnumItems()) do
textLabel.Text = enum.Name
setAllSurfaces(demoPart, enum)
task.wait(1)
end
end
BrickColor
คุณสมบัติ BrickColor กำหนดสีของส่วนหากส่วนมี BasePart.Material ก็จะกำหนดสีที่ใช้เมื่อเรนเดอร์ส่วนวัสดุเช่นกันสำหรับการควบคุมสีเพิ่มเติม คุณสามารถใช้คุณสมบัติ BasePart.Color ได้ (เป็นตัวแปรสีสีที่ 3 ของคุณสมบัตินี้)หากตั้งสีแล้ว คุณสมบัตินี้จะใช้สีอิฐที่ใกล้เคียงที่สุด
คุณสมบัติภาพอื่นๆ ของชิ้นส่วนจะถูกกำหนดโดย BasePart.Transparency และ BasePart.Reflectance
ตัวอย่างโค้ด
This code sample will allow a part to be clicked to toggle its anchored property. When toggled, the visual appearance of the part is updated (red means anchored, yellow means free).
local part = script.Parent
-- Create a ClickDetector so we can tell when the part is clicked
local cd = Instance.new("ClickDetector", part)
-- This function updates how the part looks based on its Anchored state
local function updateVisuals()
if part.Anchored then
-- When the part is anchored...
part.BrickColor = BrickColor.new("Bright red")
part.Material = Enum.Material.DiamondPlate
else
-- When the part is unanchored...
part.BrickColor = BrickColor.new("Bright yellow")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- Toggle the anchored property
part.Anchored = not part.Anchored
-- Update visual state of the brick
updateVisuals()
end
-- Update, then start listening for clicks
updateVisuals()
cd.MouseClick:Connect(onToggle)
CFrame
คุณสมบัติ CFrame กำหนดทั้งตำแหน่งและทิศทางของ BasePart ในโลกมันทำหน้าที่เป็นตำแหน่งอ้างอิงแบบสุ่มบนเรขาคณิต แต่ ExtentsCFrame เป็นตัวแทนของจุดศูนย์กลางทางกายภาพที่เป็นจริง CFrame
เมื่อตั้งค่า CFrame บนชิ้นส่วนอื่นๆ ที่เข้าร่วมจะย้ายเป็นสัดส่วนกับชิ้นส่วนด้วย แต่ขอแนะนำให้ใช้ PVInstance:PivotTo() ในการย้ายโมเดลทั้งหมด เช่น เมื่อเทเลพอร์ตตัวละครของผู้เล่น
ไม่เหมือนการตั้งค่า BasePart.Position , การตั้งค่า BasePart.CFrame จะเคลื่อนย้ายส่วนไปยังตำแหน่งที่แน่นอนที่ระบุ CFrame เสมอ; ในคำอื่น ๆ: ไม่มีการตรวจสอบการซ้อนทับที่ทำได้ และตัวแก้ปัญหาด้านฟิสิกส์จะพยายามแก้ปัญหาการซ้อนทับใด ๆ เว้นแต่ทั้งสองส่วนจะเป็น Anchored
สำหรับการติดตามตำแหน่งเมื่อเทียบกับส่วน CFrame อาจเป็นประโยชน์ถ้าใช้ Attachment
ตัวอย่างโค้ด
This code sample demonstrates setting a part's CFrame in many different ways. It showcases how to create and compose CFrame values. It references a sibling part called "OtherPart" for demonstrating relative positioning.
local part = script.Parent:WaitForChild("Part")
local otherPart = script.Parent:WaitForChild("OtherPart")
-- Reset the part's CFrame to (0, 0, 0) with no rotation.
-- This is sometimes called the "identity" CFrame
part.CFrame = CFrame.new()
-- Set to a specific position (X, Y, Z)
part.CFrame = CFrame.new(0, 25, 10)
-- Same as above, but use a Vector3 instead
local point = Vector3.new(0, 25, 10)
part.CFrame = CFrame.new(point)
-- Set the part's CFrame to be at one point, looking at another
local lookAtPoint = Vector3.new(0, 20, 15)
part.CFrame = CFrame.lookAt(point, lookAtPoint)
-- Rotate the part's CFrame by pi/2 radians on local X axis
part.CFrame = part.CFrame * CFrame.Angles(math.pi / 2, 0, 0)
-- Rotate the part's CFrame by 45 degrees on local Y axis
part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(45), 0)
-- Rotate the part's CFrame by 180 degrees on global Z axis (note the order!)
part.CFrame = CFrame.Angles(0, 0, math.pi) * part.CFrame -- Pi radians is equal to 180 degrees
-- Composing two CFrames is done using * (the multiplication operator)
part.CFrame = CFrame.new(2, 3, 4) * CFrame.new(4, 5, 6) --> equal to CFrame.new(6, 8, 10)
-- Unlike algebraic multiplication, CFrame composition is NOT communitative: a * b is not necessarily b * a!
-- Imagine * as an ORDERED series of actions. For example, the following lines produce different CFrames:
-- 1) Slide the part 5 units on X.
-- 2) Rotate the part 45 degrees around its Y axis.
part.CFrame = CFrame.new(5, 0, 0) * CFrame.Angles(0, math.rad(45), 0)
-- 1) Rotate the part 45 degrees around its Y axis.
-- 2) Slide the part 5 units on X.
part.CFrame = CFrame.Angles(0, math.rad(45), 0) * CFrame.new(5, 0, 0)
-- There is no "CFrame division", but instead simply "doing the inverse operation".
part.CFrame = CFrame.new(4, 5, 6) * CFrame.new(4, 5, 6):Inverse() --> is equal to CFrame.new(0, 0, 0)
part.CFrame = CFrame.Angles(0, 0, math.pi) * CFrame.Angles(0, 0, math.pi):Inverse() --> equal to CFrame.Angles(0, 0, 0)
-- Position a part relative to another (in this case, put our part on top of otherPart)
part.CFrame = otherPart.CFrame * CFrame.new(0, part.Size.Y / 2 + otherPart.Size.Y / 2, 0)
CanCollide
CanCollide ตรวจสอบว่าชิ้นส่วนจะโต้ตอบทางกายภาพกับชิ้นส่วนอื่นหรือไม่เมื่อปิดใช้งานแล้ว ส่วนอื่นๆ สามารถผ่านอิฐได้โดยไม่สะดุดชิ้นส่วนที่ใช้สําหรับการตกแต่ง มักจะมี CanCollide ถูกปิดใช้งานเนื่องจากไม่จําเป็นต้องพิจารณาโดยเครื่องกลศาสตร์ หากส่วนไม่ใช่ BasePart.Anchored และมีการปิดใช้งาน CanCollide แล้วอาจหลุดออกจากโลกโดย Workspace.FallenPartsDestroyHeight ในที่สุด
เมื่อ CanCollide ถูกปิดใช้งาน ชิ้นส่วนอาจยังคงเปิดใช้งานอีเวนต์ BasePart.Touched (เช่นเดียวกับชิ้นส่วนอื่นๆ ที่สัมผัสกับพวกเขา)คุณสามารถปิดใช้งานสิ่งนี้ด้วย BasePart.CanTouch
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการชนกัน ดู การชนกัน
ตัวอย่างโค้ด
This code sample shows how a part can fade away when touched by a Humanoid then reappear a moment after to create a passable door.
-- Paste into a Script inside a tall part
local part = script.Parent
local OPEN_TIME = 1
-- Can the door be opened at the moment?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("Black")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("Bright blue")
end
local function onTouch(otherPart)
-- If the door was already open, do nothing
if debounce then
print("D")
return
end
-- Check if touched by a Humanoid
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("not human")
return
end
-- Perform the door opening sequence
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()
CanQuery
CanQuery ตรวจสอบว่าส่วนนั้นถูกพิจารณาในระหว่างการดำเนินการค้นหาในพื้นที่ เช่น GetPartBoundsInBox หรือ Raycast``CanCollide จะต้องถูกปิดใช้งานเมื่อปิดใช้งาน CanQuery ด้วยฟังก์ชันเหล่านี้จะไม่รวมส่วนที่มี CanQuery และ CanCollide เป็นเท็จ
นอกเหนือจากคุณสมบัตินี้แล้วยังเป็นไปได้ที่จะยกเว้นส่วนที่เป็นลูกหลานของรายการส่วนที่กำหนดโดยใช้วัตถุ OverlapParams หรือ RaycastParams เมื่อเรียกฟังก์ชันคำถามเชิงพื้นที่
CanTouch
คุณสมบัตินี้กำหนดว่าเหตุการณ์ Touched และ TouchEnded จะเกิดขึ้นบนส่วนหรือไม่หาก true ส่วนอื่นที่สัมผัสก็ต้องมี CanTouch ตั้งค่าเป็น true เพื่อให้เหตุการณ์สัมผัสจะเกิดขึ้นหาก false ไม่สามารถตั้งเหตุการณ์แตะได้สำหรับส่วนและพยายามทำเช่นนั้นจะเกิดข้อผิดพลาดเช่นเดียวกันหากคุณสร้างคุณสมบัติเป็น false หลังจากเชื่อมต่อเหตุการณ์แตะแล้ว เหตุการณ์จะถูกตัดการเชื่อมต่อและลบ TouchTransmitter
โปรดทราบว่าโลจิสต์การชนกันนี้สามารถตั้งค่าให้เคารพ กลุ่มการชน ผ่านคุณสมบัติ Workspace.TouchesUseCollisionGroupsหาก true ส่วนในกลุ่มที่ไม่ชนกันจะเพิกเฉยทั้งการชนกัน และ เหตุการณ์สัมผัส ดังนั้นทำให้คุณสมบัตินี้ไม่เกี่ยวข้อง
ประสิทธิภาพ
มีการได้รับประสิทธิภาพเล็กน้อยในส่วนที่มีทั้ง CanTouch และ CanCollide ตั้งค่าเป็น false เนื่องจากส่วนเหล่านี้จะไม่จำเป็นต้องคำนวณชนิดของการชนกันระหว่างส่วนใดอย่างไรก็ตามพวกเขายังคงสามารถโดนโดยคำถาม Raycasts และ OverlapParams ได้
CastShadow
กำหนดว่าส่วนใดมีเงาหรือไม่
โปรดทราบว่าคุณลักษณะนี้ไม่ได้ออกแบบมาเพื่อเพิ่มประสิทธิภาพ ไม่ ควรปิดใช้งานเฉพาะในส่วนที่คุณต้องการซ่อนเงาที่ส่วนปล่อยออกมาการปิดใช้งานคุณสมบัตินี้สำหรับส่วนที่กำหนดอาจทําให้เกิดวัตถุศิลปะทางภาพบนเงาที่โยนบนส่วนนั้น
CenterOfMass
คุณสมบัติ CenterOfMass อธิบายตำแหน่ง ท้องถิ่น ของศูนย์กลางของมวลของชิ้นส่วนหากนี่เป็นการประกอบชิ้นส่วนเดียว นี่คือ AssemblyCenterOfMass ที่แปลงจากพื้นที่โลกเป็นท้องถิ่นบนง่าย Parts , ศูนย์กลางของมวลเป็นเสมอ (0,0,0)อาจแตกต่างกันได้สำหรับ WedgePart หรือ MeshPart อย่างไรก็ตาม
CollisionGroup
คุณสมบัติ CollisionGroup อธิบายชื่อกลุ่มการชนกันของชิ้นส่วน (สูงสุด 100 ตัวอักษร)ชิ้นส่วนเริ่มต้นในกลุ่มเริ่มต้นที่มีชื่อเป็น "Default"ค่านี้ไม่สามารถว่างเปล่าได้
แม้ว่าคุณสมบัตินี้จะไม่ซ้ำกัน แต่เครื่องยนต์จะสร้างค่าซ้ำภายในโดยใช้คุณสมบัติส่วนตัวอื่นเพื่อแก้ปัญหาการย้อนกลับไปข้างหลัง
ตัวอย่างโค้ด
This example demonstrates one basic use of collision groups. It assigns BallPart to "CollisionGroupBall" and DoorPart to "CollisionGroupDoor", then makes the two groups non-collidable using PhysicsService:CollisionGroupSetCollidable().
local PhysicsService = game:GetService("PhysicsService")
local collisionGroupBall = "CollisionGroupBall"
local collisionGroupDoor = "CollisionGroupDoor"
-- Register collision groups
PhysicsService:RegisterCollisionGroup(collisionGroupBall)
PhysicsService:RegisterCollisionGroup(collisionGroupDoor)
-- Assign parts to collision groups
script.Parent.BallPart.CollisionGroup = collisionGroupBall
script.Parent.DoorPart.CollisionGroup = collisionGroupDoor
-- Set groups as non-collidable with each other and check the result
PhysicsService:CollisionGroupSetCollidable(collisionGroupBall, collisionGroupDoor, false)
print(PhysicsService:CollisionGroupsAreCollidable(collisionGroupBall, collisionGroupDoor)) --> false
Color
คุณสมบัติสีจะกำหนดสีของชิ้นส่วนหากส่วนมี BasePart.Material ก็จะกำหนดสีที่ใช้เมื่อเรนเดอร์ส่วนวัสดุเช่นกันหากคุณสมบัตินี้ถูกตั้งค่า BasePart.BrickColor จะใช้สี BrickColor ที่ใกล้เคียงที่สุดกับมูลค่า Color3
คุณสมบัติภาพอื่นๆ ของชิ้นส่วนจะถูกกำหนดโดย BasePart.Transparency และ BasePart.Reflectance
ตัวอย่างโค้ด
This code sample colors a player's entire character based on how much health they have. It generates a color based on their max health, then sets the color properties of objects within their character, removing any extra objects.
-- Paste into a Script within StarterCharacterScripts
-- Then play the game, and fiddle with your character's health
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
-- Create a color by tweening based on the percentage of your health
-- The color goes from 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)
CurrentPhysicalProperties
คุณสมบัติทางกายภาพปัจจุบันบ่งบอกถึงคุณสมบัติทางกายภาพปัจจุบันของชิ้นส่วนคุณสามารถกำหนดค่าตัวเลขที่กําหนดเองสําหรับคุณสมบัติทางกายภาพต่อชิ้น, วัสดุที่กําหนดเอง และการยกเลิกวัสดุเครื่องยนต์ให้ความสำคัญกับการกำหนดค่าระดับละเอียดมากขึ้นเมื่อกำหนดคุณสมบัติทางกายภาพที่มีประสิทธิภาพของชิ้นส่วนค่าในรายการต่อไปนี้อยู่ในลำดับจากสูงสุดไปยังลําดับความสําคัญต่ําสุด:
- คุณสมบัติทางกายภาพที่กำหนดเองของชิ้นส่วน
- คุณสมบัติทางกายภาพที่กำหนดเองของวัสดุที่กำหนดเองของชิ้นส่วน
- คุณสมบัติทางกายภาพที่กำหนดเองของการเขียนทับวัสดุของชิ้นส่วน
- คุณสมบัติทางกายภาพเริ่มต้นของวัสดุของชิ้นส่วน
CustomPhysicalProperties
คุณสามารถปรับแต่งลักษณะทางกายภาพต่างๆ ของ Part ได้โดยใช้คุณสมบัติทางกายภาพที่กำหนดเอง เช่น ความหนาแน่น การเสียดสี และความยืดหยุ่น
หากเปิดใช้งานแล้ว คุณสมบัตินี้ช่วยให้คุณกำหนดค่าคุณสมบัติทางกายภาพเหล่านี้หากปิดใช้งานคุณสมบัติทางกายภาพเหล่านี้จะถูกกำหนดโดย BasePart.Material ของส่วนหน้าสำหรับ Enum.Material มีรายการวัสดุส่วนต่างๆ
ตัวอย่างโค้ด
This code sample demonstrates how to set the CustomPhysicalProperties property of a part.
local part = script.Parent
-- This will make the part light and bouncy!
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 = physProperties
EnableFluidForces
เมื่อถูกเปิดใช้งานและเมื่อ Workspace.FluidForces เปิดใช้งานแล้ว ทำให้เครื่องกลศาสตร์คำนวณแรงอากาศพลศาสตร์บน BasePart นี้
FrontSurface
คุณสมบัติพื้นผิวด้านหน้ากำหนดประเภทพื้นผิวที่ใช้สำหรับทิศทาง -Z ของชิ้นส่วนเมื่อใบหน้าของส่วนสองส่วนถูกวางไว้ข้างๆ กัน พวกเขาอาจสร้างช่องว่างระหว่างพวกเขาหากตั้งค่าเป็นมอเตอร์ BasePart.FrontSurfaceInput จะกำหนดวิธีที่ข้อต่อมอเตอร์ควรทำงาน
ประเภทพื้นผิวส่วนใหญ่จะเรนเดอร์เทกเจอร์บนใบหน้าส่วนหาก BasePart.Material ถูกตั้งค่าเป็นพลาสติกบางประเภทพื้นผิวรวมถึงบานพับ มอเตอร์ และสเตปปิ้งมอเตอร์สร้างการตกแต่ง 3D แทนหากคุณสมบัตินี้ถูกเลือกในหน้าต่างคุณสมบัติจะถูกเน้นในโลกเกมที่คล้ายกับของ SurfaceSelection
ตัวอย่างโค้ด
This code sample shows what each SurfaceType looks like on a part. In addition, it creates a BillboardGui label on the part with a TextLabel that reflects the name of the current SurfaceType.
local demoPart = script.Parent
-- Create a billboard gui to display what the current surface type is
local billboard = Instance.new("BillboardGui")
billboard.AlwaysOnTop = true
billboard.Size = UDim2.new(0, 200, 0, 50)
billboard.Adornee = demoPart
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(0, 200, 0, 50)
textLabel.BackgroundTransparency = 1
textLabel.TextStrokeTransparency = 0
textLabel.TextColor3 = Color3.new(1, 1, 1) -- White
textLabel.Parent = billboard
billboard.Parent = demoPart
local function setAllSurfaces(part, surfaceType)
part.TopSurface = surfaceType
part.BottomSurface = surfaceType
part.LeftSurface = surfaceType
part.RightSurface = surfaceType
part.FrontSurface = surfaceType
part.BackSurface = surfaceType
end
while true do
-- Iterate through the different SurfaceTypes
for _, enum in pairs(Enum.SurfaceType:GetEnumItems()) do
textLabel.Text = enum.Name
setAllSurfaces(demoPart, enum)
task.wait(1)
end
end
LeftSurface
คุณสมบัติพื้นผิวด้านซ้ายกำหนดประเภทพื้นผิวที่ใช้สำหรับทิศทาง -X ของชิ้นส่วนเมื่อใบหน้าของส่วนสองส่วนถูกวางไว้ข้างๆ กัน พวกเขาอาจสร้างช่องว่างระหว่างพวกเขาหากตั้งค่าเป็นมอเตอร์ BasePart.LeftSurfaceInput จะกำหนดวิธีที่ข้อต่อมอเตอร์ควรทำงาน
ประเภทพื้นผิวส่วนใหญ่จะเรนเดอร์เทกเจอร์บนใบหน้าส่วนหาก BasePart.Material ถูกตั้งค่าเป็นพลาสติกบางประเภทพื้นผิวรวมถึงบานพับ มอเตอร์ และสเตปปิ้งมอเตอร์สร้างการตกแต่ง 3D แทนหากคุณสมบัตินี้ถูกเลือกในหน้าต่างคุณสมบัติจะถูกเน้นในโลกเกมที่คล้ายกับของ SurfaceSelection
ตัวอย่างโค้ด
This code sample shows what each SurfaceType looks like on a part. In addition, it creates a BillboardGui label on the part with a TextLabel that reflects the name of the current SurfaceType.
local demoPart = script.Parent
-- Create a billboard gui to display what the current surface type is
local billboard = Instance.new("BillboardGui")
billboard.AlwaysOnTop = true
billboard.Size = UDim2.new(0, 200, 0, 50)
billboard.Adornee = demoPart
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(0, 200, 0, 50)
textLabel.BackgroundTransparency = 1
textLabel.TextStrokeTransparency = 0
textLabel.TextColor3 = Color3.new(1, 1, 1) -- White
textLabel.Parent = billboard
billboard.Parent = demoPart
local function setAllSurfaces(part, surfaceType)
part.TopSurface = surfaceType
part.BottomSurface = surfaceType
part.LeftSurface = surfaceType
part.RightSurface = surfaceType
part.FrontSurface = surfaceType
part.BackSurface = surfaceType
end
while true do
-- Iterate through the different SurfaceTypes
for _, enum in pairs(Enum.SurfaceType:GetEnumItems()) do
textLabel.Text = enum.Name
setAllSurfaces(demoPart, enum)
task.wait(1)
end
end
LocalTransparencyModifier
คุณสมบัติ LocalTransparencyModifier เป็นตัวคูณสำหรับ BasePart.Transparency ที่มองเห็นได้เฉพาะกับไคลเอนต์ท้องถิ่นเท่านั้นมันไม่ได้สําเนาจากไคลเอนต์ไปยังเซิร์ฟเวอร์และมีประโยชน์เมื่อส่วนหนึ่งไม่ควรแสดงผลสําหรับไคลเอนต์เฉพาะรายหนึ่ง เช่น เมื่อผู้เล่นไม่เห็นส่วนของร่างกายของตัวละครของพวกเขาเมื่อพวกเขาซูมเข้าสู่โหมดบุคคลที่หนึ่ง
คุณสมบัตินี้แก้ไขความโปร่งใสของส่วนท้องถิ่นผ่านสูตรต่อไปนี้โดยมีค่าที่ได้รับระหว่าง 0 และ 1
clientTransparency = 1 - ((1 - part.Transparency) * (1 - part.LocalTransparencyModifier))
<th>ตัวแก้ไขความโปร่งใสท้องถิ่น</th><th>ความโปร่งใสด้านเซิร์ฟเวอร์</th><th>ความโปร่งใสด้านลูกค้า</th></tr></thead><tbody><tr><td>0.5</td><td>0</td><td>0.5</td><td>0.5</td></tr><tr><td>0.5</td><td>0.25</td><td>0.5</td><td>0.625</td></tr><tr><td>0.5</td><td>0.5</td><td>0.5</td><td>0.75</td></tr><tr><td>0.5</td><td>0.75</td><td>0.5</td><td>0.875</td></tr><tr><td>0.5</td><td>1</td><td>0.5</td><td>1</td></tr></tbody>
ความโปร่งใส |
---|
Locked
คุณสมบัติล็อคจะกำหนดว่าคุณสมบัติ part (หรือ model ถ้ามันอยู่ภายใน) สามารถเลือกได้ใน Roblox Studio โดยคลิกที่มันคุณสมบัตินี้มักจะเปิดใช้งานบ่อยที่สุดในส่วนภายในโมเดลสภาพแวดล้อมที่ไม่ได้แก้ไขในขณะนี้โรบล็อกสตูดิโอมีเครื่องมือล็อค/ปลดล็อคทั้งหมดที่สามารถสลับสถานะล็อคของทุกส่วนย่อยในรูปแบบได้ในครั้งเดียว
ตัวอย่างโค้ด
This code sample uses the concept of recursion to unlock all parts that are a descendant of a model.
-- Paste into a Script within a Model you want to unlock
local model = script.Parent
-- This function recurses through a model's heirarchy and unlocks
-- every part that it encounters.
local function recursiveUnlock(object)
if object:IsA("BasePart") then
object.Locked = false
end
-- Call the same function on the children of the object
-- The recursive process stops if an object has no children
for _, child in pairs(object:GetChildren()) do
recursiveUnlock(child)
end
end
recursiveUnlock(model)
Mass
มวล เป็นคุณสมบัติอ่านได้ที่อธิบายถึงผลิตภัณฑ์ของปริมาณและความหนาแน่นของชิ้นส่วน มันจะถูกส่งคืนโดยฟังก์ชัน GetMass
- ความหนาแน่นของชิ้นส่วนจะถูกกำหนดโดย Material หรือ CustomPhysicalProperties ถ้าระบุ
Massless
หากคุณสมบัตินี้เปิดใช้งานแล้ว BasePart จะไม่มีส่วนร่วมในมวลรวมหรือแรงเฉื่อยของการประกอบจนกว่าจะถูกเชื่อมต่อกับส่วนอื่นที่มีมวล
หากส่วนเป็นส่วนรากของตัวเองตาม AssemblyRootPart ส่วนนี้จะถูกเพิกเฉยสำหรับส่วนนั้นและจะยังคงมีส่วนร่วมในมวลและแรงเฉื่อยในการประกอบของมันเช่นเดียวกับส่วนปกติชิ้นส่วนที่ไม่มีมวลไม่ควรกลายเป็นส่วนรากของการประกอบไม่ว่าจะเป็นส่วนอื่นๆ ในการประกอบทั้งหมดหรือไม่
นี่อาจมีประโยชน์สำหรับสิ่งเช่นอุปกรณ์เสริมที่ไม่จำเป็นบนยานพาหนะที่คุณไม่ต้องการที่จะส่งผลต่อการจัดการของรถหรือเมชรวมที่ไม่มีมวลเชื่อมต่อกับเมชการชนที่เรียบง่าย
ดูเพิ่มเติม ชิ้นส่วนรวม บทความที่อธิบายสิ่งที่เป็นส่วนรากและวิธีการใช้งาน
Material
คุณสมบัติวัสดุช่วยให้นักสร้างสามารถตั้งเนื้อสัมผัสและคุณสมบัติทางกายภาพเริ่มต้นของชิ้นส่วน (ในกรณีที่ BasePart.CustomPhysicalProperties ไม่ได้ถูกตั้งค่า)วัสดุพลาสติกเริ่มต้นมีเนื้อสัมผัสที่เบามาก และวัสดุ SmoothPlastic ไม่มีเนื้อสัมผัสเลยเทกเจอร์วัสดุบางอย่างเช่น DiamondPlate และ Granite มีเทกเจอร์ที่เห็นได้ชัดเจนมากเทกเจอร์ของวัสดุแต่ละชนิดสะท้อนแสงแดดแตกต่างกัน โดยเฉพาะอย่างยิ่งฟอยล์
การตั้งค่าคุณสมบัตินี้จากนั้นเปิดใช้งาน BasePart.CustomPhysicalProperties อินสแตนซ์DiamondPlate เป็นวัสดุหนาแน่นมากในขณะที่ไม้เป็นวัสดุที่เบามากความหนาแน่นของชิ้นส่วนจะกำหนดว่าจะลอยในน้ําในพื้นที่หรือไม่
พฤติกรรมการเปลี่ยนแปลงวัสดุแก้วเปลี่ยนการแสดงผลในการตั้งค่ากราฟิกปานกลางมันใช้ความสะท้อนแสงเล็กน้อย (คล้ายกับ BasePart.Reflectance ) และการเปลี่ยนรูปมุมมองผลกระทบจะเด่นชัดบนชิ้นทรงกลม (ตั้ง BasePart.Shape เป็นลูกบอล)วัตถุเซมิโปร่งใสและชิ้นส่วนแก้วที่อยู่เบื้องหลังกระจกไม่สามารถมองเห็นได้
ตัวอย่างโค้ด
This code sample will allow a part to be clicked to toggle its anchored property. When toggled, the visual appearance of the part is updated (red means anchored, yellow means free).
local part = script.Parent
-- Create a ClickDetector so we can tell when the part is clicked
local cd = Instance.new("ClickDetector", part)
-- This function updates how the part looks based on its Anchored state
local function updateVisuals()
if part.Anchored then
-- When the part is anchored...
part.BrickColor = BrickColor.new("Bright red")
part.Material = Enum.Material.DiamondPlate
else
-- When the part is unanchored...
part.BrickColor = BrickColor.new("Bright yellow")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- Toggle the anchored property
part.Anchored = not part.Anchored
-- Update visual state of the brick
updateVisuals()
end
-- Update, then start listening for clicks
updateVisuals()
cd.MouseClick:Connect(onToggle)
MaterialVariant
ระบบค้นหาตัวอย่าง MaterialVariant ด้วยชื่อ MaterialVariant พิมพ์BasePart.Materialหากพบสิ่งที่ตรงกันได้สำเร็จในตัวแปรวัสดุที่ตรงกัน จะใช้ตัวแปรวัสดุนี้เพื่อแทนที่วัสดุเริ่มต้นวัสดุเริ่มต้นสามารถเป็นวัสดุที่ติดตั้งไว้หรือยกเลิกวัสดุประเภทที่กำหนดไว้ใน MaterialService
Orientation
คุณสมบัติการจัดตำแหน่งอธิบายการหมุนส่วนในรอบแกน X, Y และ Z โดยใช้ Vector3การหมุนจะถูกใช้ในลำดับ Y → X → Zนี้แตกต่างจาก มุม Euler ที่เหมาะสม และแทนที่ด้วย มุม Tait-Bryan ซึ่งอธิบายถึง แรงบิด แนวนอน และการกลิ้ง ยังคุ้มค่าที่จะสังเกตว่าคุณสมบัตินี้แตกต่างจากคอนสตรัคเตอร์ CFrame.Angles() ซึ่งใช้การหมุนในลำดับที่แตกต่างกัน (Z → Y → X)เพื่อการควบคุมที่ดีกว่าในการหมุนชิ้นส่วน ขอแนะนำให้ตั้ง BasePart.CFrame แทน
เมื่อตั้งค่าคุณสมบัตินี้ คุณสมบัติใด ๆ Welds หรือ Motor6Ds ที่เชื่อมต่อกับส่วนนี้จะมีคุณสมบัติที่ตรงกัน C0 หรือ C1 ที่อัปเดตและอนุญาตให้ส่วนย้ายเป็นสัดส่วนกับส่วนอื่นที่มันเชื่อมต่อ
ข้อจํากัดการเชื่อมต่อจะถูกปิดใช้งานชั่วคราวและเปิดใช้งานใหม่ในระหว่างการย้าย
ตัวอย่างโค้ด
This code sample rotates a part continually on the Y axis.
local part = script.Parent
local INCREMENT = 360 / 20
-- Rotate the part continually
while true do
for degrees = 0, 360, INCREMENT do
-- Set only the Y axis rotation
part.Rotation = Vector3.new(0, degrees, 0)
-- A better way to do this would be setting CFrame
--part.CFrame = CFrame.new(part.Position) * CFrame.Angles(0, math.rad(degrees), 0)
task.wait()
end
end
PivotOffset
คุณสมบัตินี้กำหนดค่าออฟเซ็ตของจุดศูนย์กลางของชิ้นส่วนจาก CFrame ซึ่งเป็น part:GetPivot() เท่ากับ part.CFrame * part.PivotOffset
สะดวกสำหรับการตั้งค่าจุดศูนย์กลางไปยังตำแหน่งในพื้นที่ ท้องถิ่น แต่การตั้งค่าจุดศูนย์กลางของชิ้นส่วนไปยังตำแหน่งในพื้นที่ โลก สามารถทำได้ดังนี้:
local Workspace = game:GetService("Workspace")local part = Workspace.BluePartlocal desiredPivotCFrameInWorldSpace = CFrame.new(0, 10, 0)part.PivotOffset = part.CFrame:ToObjectSpace(desiredPivotCFrameInWorldSpace)
ตัวอย่างโค้ด
This code sample shows a custom function for resetting the pivot of a model back to the center of that model's bounding box.
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)
This code sample creates a clock at the origin with a minute, second, and hour hand, and makes it tick, displaying the local time.
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
-- Create dial
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
-- Create hands
local hourHand = createHand(7, 1, 0)
local minuteHand = createHand(10, 0.6, 0.1)
local secondHand = createHand(11, 0.2, 0.2)
-- Run clock
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()
end
Position
คุณสมบัติตำแหน่งอธิบายพิกัดของ part โดยใช้ Vector3 ในการใช้งานมันสะท้อนตำแหน่งของส่วน BasePart.CFrame แต่ยังสามารถตั้งค่าได้
เมื่อตั้งค่าคุณสมบัตินี้ คุณสมบัติใด ๆ Welds หรือ Motor6Ds ที่เชื่อมต่อกับส่วนนี้จะมีคุณสมบัติที่ตรงกัน C0 หรือ C1 ที่อัปเดตและอนุญาตให้ส่วนย้ายเป็นสัดส่วนกับส่วนอื่นที่มันเชื่อมต่อ
ข้อจํากัดการเชื่อมต่อจะถูกปิดใช้งานชั่วคราวและเปิดใช้งานใหม่ในระหว่างการย้าย
ReceiveAge
สิ่งนี้จะคืนเวลาในวินาทีตั้งแต่ฟิสิกส์ของชิ้นส่วนได้รับการปรับปรุงล่าสุดบนไคลเอนต์ท้องถิ่น (หรือเซิร์ฟเวอร์)ส่งคืน 0 เมื่อชิ้นส่วนไม่มีฟิสิกส์ (ถูกยึด)
Reflectance
คุณสมบัติการสะท้อนกำหนดจำนวนที่ part สะท้อนกล่องท้องฟ้าค่า 0 ทั้งหมดและค่า 1 บ่งบอกว่าส่วนนั้นควรสะท้อนอย่างเต็มที่
ความสะท้อนไม่ได้รับผลกระทบจาก BasePart.Transparency เว้นแต่ชิ้นส่วนจะโปร่งใสอย่างสมบูรณ์ในกรณีนั้นความสะท้อนจะไม่แสดงเลยความสะท้อนแสงอาจถูกเพิกเฉยหรือไม่อาจถูกเพิกเฉยขึ้นอยู่กับ BasePart.Material ของส่วน
ตัวอย่างโค้ด
This code sample causes a part to blink its Reflectance and a PointLight every time it is touched. It uses a pattern that prevents multiple concurrent function calls from fighting with each other.
local part = script.Parent
local pointLight = Instance.new("PointLight")
pointLight.Brightness = 0
pointLight.Range = 12
pointLight.Parent = part
local touchNo = 0
local function blink()
-- Advance touchNo to tell other blink() calls to stop early
touchNo = touchNo + 1
-- Save touchNo locally so we can tell when it changes globally
local myTouchNo = touchNo
for i = 1, 0, -0.1 do
-- Stop early if another blink started
if touchNo ~= myTouchNo then
break
end
-- Update the blink animation
part.Reflectance = i
pointLight.Brightness = i * 2
task.wait(0.05)
end
end
part.Touched:Connect(blink)
ResizeIncrement
คุณสมบัติ ResizeIncrement เป็นคุณสมบัติอ่านเฉพาะที่อธิบายการเปลี่ยนแปลงขนาดเล็กสุดที่อนุญาตโดยวิธี BasePart:Resize()มันแตกต่างกันระหว่างการใช้งานของคลาส abstract อินสแตนซ์ มีการตั้งค่านี้เป็น 1 และ มีการตั้งค่านี้เป็น 2 (เนื่องจากส่วนตรุสแต่ละส่วนมีขนาด 2x2x2)
ตัวอย่างโค้ด
This code sample creates a Handles object and shows how to set the Faces property of the object. It also references ResizeableFaces of a part. Try placing this script in multiple kinds of parts to see how ResizeableFaces varies.
-- Put this Script in several kinds of BasePart, like
-- Part, TrussPart, WedgePart, CornerWedgePart, etc.
local part = script.Parent
-- Create a handles object for this part
local handles = Instance.new("Handles")
handles.Adornee = part
handles.Parent = part
-- Manually specify the faces applicable for this handle
handles.Faces = Faces.new(Enum.NormalId.Top, Enum.NormalId.Front, Enum.NormalId.Left)
-- Alternatively, use the faces on which the part can be resized.
-- If part is a TrussPart with only two Size dimensions
-- of length 2, then ResizeableFaces will only have two
-- enabled faces. For other parts, all faces will be enabled.
handles.Faces = part.ResizeableFaces
ResizeableFaces
คุณสมบัติ ResizableFaces (พร้อมด้วย e ไม่ใช่ ResizableFaces) อธิบายถึงการใช้วัตถุใบหน้าที่สามารถปรับขนาดได้สำหรับใบหน้าที่แตกต่างกันซึ่งส่วนหนึ่งอาจปรับขนาดได้สำหรับการใช้งานส่วนใหญ่ของ BasePart , เช่น Part และ WedgePart คุณสมบัตินี้รวมถึงใบหน้าทั้งหมดอย่างไรก็ตาม TrussPart จะตั้งค่า ResizableFaces เป็นสองใบหน้าเท่านั้นเนื่องจากชิ้นส่วนเหล่านี้ต้องมีมิติความยาวสอง BasePart.Size เท่าคุณสมบัตินี้มักใช้กับเครื่องมือที่ใช้สำหรับการสร้างและปรับแต่งชิ้นส่วนและมีการใช้งานน้อยนอกเหนือจากบริบทนั้นคลาส Handles ซึ่งมีคุณสมบัติ Handles.Faces สามารถใช้ร่วมกับคุณสมบัตินี้เพื่อแสดงเฉพาะจัดการบนใบหน้าที่สามารถปรับขนาดได้บนชิ้นส่วน
ตัวอย่างโค้ด
This code sample creates a Handles object and shows how to set the Faces property of the object. It also references ResizeableFaces of a part. Try placing this script in multiple kinds of parts to see how ResizeableFaces varies.
-- Put this Script in several kinds of BasePart, like
-- Part, TrussPart, WedgePart, CornerWedgePart, etc.
local part = script.Parent
-- Create a handles object for this part
local handles = Instance.new("Handles")
handles.Adornee = part
handles.Parent = part
-- Manually specify the faces applicable for this handle
handles.Faces = Faces.new(Enum.NormalId.Top, Enum.NormalId.Front, Enum.NormalId.Left)
-- Alternatively, use the faces on which the part can be resized.
-- If part is a TrussPart with only two Size dimensions
-- of length 2, then ResizeableFaces will only have two
-- enabled faces. For other parts, all faces will be enabled.
handles.Faces = part.ResizeableFaces
RightSurface
คุณสมบัติพื้นผิวด้านขวากำหนดประเภทพื้นผิวที่ใช้สำหรับทิศทาง +X ของชิ้นส่วนเมื่อใบหน้าของส่วนสองส่วนถูกวางไว้ข้างๆ กัน พวกเขาอาจสร้างช่องว่างระหว่างพวกเขาหากตั้งค่าเป็นมอเตอร์ BasePart.RightSurfaceInput จะกำหนดวิธีที่ข้อต่อมอเตอร์ควรทำงาน
ประเภทพื้นผิวส่วนใหญ่จะเรนเดอร์เทกเจอร์บนใบหน้าส่วนหาก BasePart.Material ถูกตั้งค่าเป็นพลาสติกบางประเภทพื้นผิวรวมถึงบานพับ มอเตอร์ และมอเตอร์ก้าวจะแสดงการตกแต่ง 3D แทนหากคุณสมบัตินี้ถูกเลือกในหน้าต่างคุณสมบัติจะถูกเน้นในโลกเกมที่คล้ายกับของ SurfaceSelection
ตัวอย่างโค้ด
This code sample shows what each SurfaceType looks like on a part. In addition, it creates a BillboardGui label on the part with a TextLabel that reflects the name of the current SurfaceType.
local demoPart = script.Parent
-- Create a billboard gui to display what the current surface type is
local billboard = Instance.new("BillboardGui")
billboard.AlwaysOnTop = true
billboard.Size = UDim2.new(0, 200, 0, 50)
billboard.Adornee = demoPart
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(0, 200, 0, 50)
textLabel.BackgroundTransparency = 1
textLabel.TextStrokeTransparency = 0
textLabel.TextColor3 = Color3.new(1, 1, 1) -- White
textLabel.Parent = billboard
billboard.Parent = demoPart
local function setAllSurfaces(part, surfaceType)
part.TopSurface = surfaceType
part.BottomSurface = surfaceType
part.LeftSurface = surfaceType
part.RightSurface = surfaceType
part.FrontSurface = surfaceType
part.BackSurface = surfaceType
end
while true do
-- Iterate through the different SurfaceTypes
for _, enum in pairs(Enum.SurfaceType:GetEnumItems()) do
textLabel.Text = enum.Name
setAllSurfaces(demoPart, enum)
task.wait(1)
end
end
RootPriority
คุณสมบัตินี้เป็นตัวเลขที่อยู่ระหว่าง -127 และ 127 ที่มีลําดับความสําคัญสูงกว่ากฎอื่น ๆ ทั้งหมดสําหรับการจัดเรียงส่วนรากเมื่อพิจารณาส่วนหลายส่วนที่ไม่ใช่ และมีมูลค่า เดียวกัน ส่วนที่มี ลําดับความสําคัญรากสูงกว่าจะได้รับความสําคัญมากกว่าส่วนที่มี ลําดับความสําคัญรากต่ํากว่า
คุณสามารถใช้คุณสมบัตินี้เพื่อควบคุมส่วนใดของการประกอบที่เป็นส่วนรากและรักษาส่วนรากให้มีความเสถียรหากมีการเปลี่ยนแปลงขนาด
ดูเพิ่มเติม ชิ้นส่วนรวม บทความที่อธิบายสิ่งที่เป็นส่วนรากและวิธีการใช้งาน
Rotation
การหมุนของชิ้นส่วนในองศาสำหรับสามแกน
เมื่อตั้งค่าคุณสมบัตินี้ คุณสมบัติใด ๆ Welds หรือ Motor6Ds ที่เชื่อมต่อกับส่วนนี้จะมีคุณสมบัติที่ตรงกัน C0 หรือ C1 ที่อัปเดตและอนุญาตให้ส่วนย้ายเป็นสัดส่วนกับส่วนอื่นที่มันเชื่อมต่อ
ข้อจํากัดการเชื่อมต่อจะถูกปิดใช้งานชั่วคราวและเปิดใช้งานใหม่ในระหว่างการย้าย
Size
คุณสมบัติของชิ้นส่วน Size กำหนดขนาดมุมมองของมัน **** ในขณะที่ ExtentsSize แทนที่ขนาดจริงที่ใช้โดยเครื่องกลศาสตร์ เช่นใน การตรวจจับการชนมิติแต่ละมิติ (ความยาว ความกว้าง ความสูง) สามารถต่ำเป็น 0.001 และสูงถึง 2048ขนาดมิติด้านล่าง 0.05 จะเป็น ทางสายตา ถูกแสดงเป็นถ้าขนาดมิติของชิ้นส่วนเป็น 0.05
ขนาดของชิ้นส่วนกำหนดมวลซึ่งได้รับโดย BasePart:GetMass() ชิ้นส่วนของ Size ถูกใช้โดยวัตถุอื่นๆ หลากหลาย:
- ParticleEmitter เพื่อกำหนดพื้นที่ที่อนุภาคถูกสร้างขึ้น
- BlockMesh เพื่อกำหนดส่วนหนึ่งของปริซึมสี่เหลี่ยมที่เรนเดอร์ได้รับบางส่วน
- SpecialMesh สำหรับบาง MeshTypes เพื่อกำหนดขนาดของเมชที่เรนเดอร์ได้รับ
- SurfaceLight เพื่อกำหนดพื้นที่ที่จะส่องแสง
ตัวอย่างโค้ด
This code sample constructs a pyramid by stacking parts that get progressively smaller. It also colors the parts so they blend between a start color and end color.
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
-- Tween from color0 and 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 = workspace
TopSurface
คุณสมบัติ TopSurface กำหนดประเภทพื้นผิวที่ใช้สำหรับทิศทาง +Y ของชิ้นส่วนเมื่อใบหน้าของส่วนสองส่วนถูกวางไว้ข้างๆ กัน พวกเขาอาจสร้างช่องว่างระหว่างพวกเขาหากตั้งค่าเป็นมอเตอร์ BasePart.TopSurfaceInput จะกำหนดวิธีที่ข้อต่อมอเตอร์ควรทำงาน
ประเภทพื้นผิวส่วนใหญ่จะเรนเดอร์เทกเจอร์บนใบหน้าส่วนหาก BasePart.Material ถูกตั้งค่าเป็นพลาสติกบางประเภทพื้นผิว - บานพับ มอเตอร์ และมอเตอร์ก้าว - จะแสดงการตกแต่ง 3D แทนหากคุณสมบัตินี้ถูกเลือกในหน้าต่างคุณสมบัติจะถูกเน้นในโลกเกมที่คล้ายกับของ SurfaceSelection
ตัวอย่างโค้ด
This code sample shows what each SurfaceType looks like on a part. In addition, it creates a BillboardGui label on the part with a TextLabel that reflects the name of the current SurfaceType.
local demoPart = script.Parent
-- Create a billboard gui to display what the current surface type is
local billboard = Instance.new("BillboardGui")
billboard.AlwaysOnTop = true
billboard.Size = UDim2.new(0, 200, 0, 50)
billboard.Adornee = demoPart
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(0, 200, 0, 50)
textLabel.BackgroundTransparency = 1
textLabel.TextStrokeTransparency = 0
textLabel.TextColor3 = Color3.new(1, 1, 1) -- White
textLabel.Parent = billboard
billboard.Parent = demoPart
local function setAllSurfaces(part, surfaceType)
part.TopSurface = surfaceType
part.BottomSurface = surfaceType
part.LeftSurface = surfaceType
part.RightSurface = surfaceType
part.FrontSurface = surfaceType
part.BackSurface = surfaceType
end
while true do
-- Iterate through the different SurfaceTypes
for _, enum in pairs(Enum.SurfaceType:GetEnumItems()) do
textLabel.Text = enum.Name
setAllSurfaces(demoPart, enum)
task.wait(1)
end
end
Transparency
คุณสมบัติความโปร่งใสควบคุมการมองเห็นของส่วนบนเครื่องชั่ง 0 ถึง 1 โดยที่ 0 สามารถมองเห็นได้อย่างสมบูรณ์ (ทึบ) และค่าของ 1 สามารถมองไม่เห็นได้อย่างสมบูรณ์ (ทั้งหมด)
BasePart.Reflectance สามารถลดความโปร่งใสโดยรวมของอิฐถ้าตั้งค่าเป็นค่าใกล้เคียงกับ 1
การดำเนินการวัตถุโปร่งใสบางส่วนมีค่าใช้จ่ายในการแสดงผลที่สำคัญบางอย่างการมีส่วนใสหลายส่วนอาจทำให้ประสิทธิภาพของเกมช้าลง
เมื่อชิ้นส่วนโปร่งใสซ้อนทับกัน คำสั่งรender สามารถกระทำได้ไม่สามารถคาดการณ์ได้ - ลองหลีกเลี่ยงการซ้อนชิ้นส่วนครึ่งโปร่งใสเพื่อหลีกเลี่ยงสิ่งนี้
The BasePart.LocalTransparencyModifier เป็นตัวคูณความโปร่งใสที่มองเห็นได้เฉพาะกับไคลเอนต์ท้องถิ่นเท่านั้น
ตัวอย่างโค้ด
This code sample shows how a part can fade away when touched by a Humanoid then reappear a moment after to create a passable door.
-- Paste into a Script inside a tall part
local part = script.Parent
local OPEN_TIME = 1
-- Can the door be opened at the moment?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("Black")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("Bright blue")
end
local function onTouch(otherPart)
-- If the door was already open, do nothing
if debounce then
print("D")
return
end
-- Check if touched by a Humanoid
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("not human")
return
end
-- Perform the door opening sequence
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()
This code sample gives the local client X-ray vision using LocalTransparencyModifier. It allows the player to see through all parts in the Workspace, which are found using recursion.
local function makeXRayPart(part)
-- LocalTransparencyModifier will make parts see-through but only for the local
-- client, and it won't replicate to the server
part.LocalTransparencyModifier = 0.5
end
-- This function uses recursion to search for parts in the game
local function recurseForParts(object)
if object:IsA("BasePart") then
makeXRayPart(object)
end
-- Stop if this object has a Humanoid - we don't want to see-through players!
if object:FindFirstChildOfClass("Humanoid") then
return
end
-- Check the object's children for more parts
for _, child in pairs(object:GetChildren()) do
recurseForParts(child)
end
end
recurseForParts(workspace)
วิธีการ
AngularAccelerationToTorque
พารามิเตอร์
ส่งค่ากลับ
ApplyAngularImpulse
ใช้ช่วงแรงแบบเฉียบพลันกับการประกอบชิ้นส่วนนี้ทําให้การประกอบหมุน
ความเร็วในมุมที่ได้จากการชนกระแทกขึ้นอยู่กับ mass ของการประกอบดังนั้นจึงจำเป็นต้องมีแรงกระตุ้นสูงขึ้นเพื่อย้ายชิ้นส่วนขนาดใหญ่มากขึ้นแรงผลักดันมีประโยชน์สำหรับกรณีที่คุณต้องการให้แรงถูกใช้ทันที เช่น การระเบิดหรือการชนกัน
หากส่วนเป็นเจ้าของ โดยเซิร์ฟเวอร์ ฟังก์ชันนี้ต้องเรียกจากเซิร์ฟเวอร์ (ไม่ใช่จาก หรือ ที่ตั้งค่าไว้เป็น )หากส่วนเป็นของลูกค้าผ่านการเป็นเจ้าของอัตโนมัติ ผ่านการเป็นเจ้าของ ฟังก์ชันนี้สามารถเรียกได้จากสคริปต์ลูกค้าหรือสคริปต์เซิร์ฟเวอร์; การเรียกสคริปต์ลูกค้าสำหรับส่วนที่เป็นเจ้าของของเซิร์ฟเวอร์จะไม่มีผล
พารามิเตอร์
เวกเตอร์แรงกระตุกแบบมุมที่จะใช้กับการประกอบ
ส่งค่ากลับ
ApplyImpulse
ฟังก์ชันนี้ใช้แรงกระแทกทันทีกับการประกอบชิ้นส่วนนี้
แรงถูกใช้ที่การประกอบ center of mass ดังนั้นการเคลื่อนไหวที่เกิดขึ้นจะเป็นเชิงเส้นเท่านั้น
ความเร็วที่ได้จากช่วงเวลาสั้นๆ ขึ้นอยู่กับ mass ของการประกอบดังนั้นจึงจำเป็นต้องมีแรงกระตุ้นสูงขึ้นเพื่อย้ายชิ้นส่วนขนาดใหญ่มากขึ้นแรงผลักดันมีประโยชน์สำหรับกรณีที่คุณต้องการให้แรงถูกใช้ทันที เช่น การระเบิดหรือการชนกัน
หากส่วนเป็นเจ้าของ โดยเซิร์ฟเวอร์ ฟังก์ชันนี้ต้องเรียกจากเซิร์ฟเวอร์ (ไม่ใช่จาก หรือ ที่ตั้งค่าไว้เป็น )หากส่วนเป็นของลูกค้าผ่านการเป็นเจ้าของอัตโนมัติ ผ่านการเป็นเจ้าของ ฟังก์ชันนี้สามารถเรียกได้จากสคริปต์ลูกค้าหรือสคริปต์เซิร์ฟเวอร์; การเรียกสคริปต์ลูกค้าสำหรับส่วนที่เป็นเจ้าของของเซิร์ฟเวอร์จะไม่มีผล
พารามิเตอร์
เวกเตอร์แรงกระตุกเชิงเส้นที่จะใช้กับการประกอบ
ส่งค่ากลับ
ApplyImpulseAtPosition
ฟังก์ชันนี้ใช้แรงกระแทกทันทีกับการประกอบชิ้นส่วนนี้ในตําแหน่งที่กำหนดในพื้นที่โลก
หากตำแหน่งไม่อยู่ที่การประกอบ center of mass แรงกระตุกจะทําให้เกิดการเคลื่อนที่ในแนวและการหมุน
ความเร็วที่ได้จากช่วงเวลาสั้นๆ ขึ้นอยู่กับ mass ของการประกอบดังนั้นจึงจำเป็นต้องมีแรงกระตุ้นสูงขึ้นเพื่อย้ายชิ้นส่วนขนาดใหญ่มากขึ้นช่วงกระตุ้นมีประโยชน์สำหรับกรณีที่นักพัฒนาต้องการใช้แรงในทันที เช่น การระเบิดหรือการชนกัน
หากส่วนเป็นเจ้าของ โดยเซิร์ฟเวอร์ ฟังก์ชันนี้ต้องเรียกจากเซิร์ฟเวอร์ (ไม่ใช่จาก หรือ ที่ตั้งค่าไว้เป็น )หากส่วนเป็นของลูกค้าผ่านการเป็นเจ้าของอัตโนมัติ ผ่านการเป็นเจ้าของ ฟังก์ชันนี้สามารถเรียกได้จากสคริปต์ลูกค้าหรือสคริปต์เซิร์ฟเวอร์; การเรียกสคริปต์ลูกค้าสำหรับส่วนที่เป็นเจ้าของของเซิร์ฟเวอร์จะไม่มีผล
พารามิเตอร์
เวกเตอร์แรงกระตุ้นที่จะใช้กับการประกอบ
ตำแหน่งในพื้นที่โลกเพื่อใช้แรงกระตุ้น
ส่งค่ากลับ
CanCollideWith
ส่งคืนว่าชิ้นส่วนสามารถชนกันได้หรือไม่ฟังก์ชันนี้พิจารณากลุ่มการชนกันของส่วนทั้งสองฟังก์ชันนี้จะผิดพลาดหากส่วนที่ระบุไม่ใช่ส่วนฐาน
พารามิเตอร์
ส่วนที่ระบุถูกตรวจสอบเพื่อความชนกันได้
ส่งค่ากลับ
ว่าชิ้นส่วนสามารถชนกันได้หรือไม่
CanSetNetworkOwnership
ฟังก์ชัน CanSetNetworkOwnership ตรวจสอบว่าคุณสามารถตั้งค่าการเป็นเจ้าของเครือข่ายส่วนหนึ่งได้หรือไม่
ค่าการคืนฟังก์ชันตรวจสอบว่าคุณสามารถโทรไปที่ BasePart:SetNetworkOwner() หรือ BasePart:SetNetworkOwnershipAuto() โดยไม่พบข้อผิดพลาดหรือไม่มันจะส่งคืนค่าจริงหากคุณสามารถแก้ไข/อ่านการเป็นเจ้าของเครือข่ายได้หรือไม่ หรือส่งคืนค่าเท็จและเหตุผลที่คุณไม่สามารถทำได้เป็นข้อความ
ส่งค่ากลับ
ว่าคุณสามารถแก้ไขหรืออ่านเจ้าของเครือข่ายและเหตุผลได้หรือไม่
ตัวอย่างโค้ด
This example checks whether or not the network ownership of the first BasePart named Part in the Workspace can be set.
local part = workspace:FindFirstChild("Part")
if part and part:IsA("BasePart") then
local canSet, errorReason = part:CanSetNetworkOwnership()
if canSet then
print(part:GetFullName() .. "'s Network Ownership can be changed!")
else
warn("Cannot change the Network Ownership of " .. part:GetFullName() .. " because: " .. errorReason)
end
end
GetConnectedParts
คืนตารางชิ้นส่วนที่เชื่อมโยงกับวัตถุโดยใช้ข้อต่อแข็งใดๆ
หาก recursive เป็นจริงฟังก์ชันนี้จะคืนชิ้นส่วนทั้งหมดในการประกอบที่เชื่อมต่อกับ BasePart อย่างเคร่งครัด
ข้อต่อแข็ง
เมื่อขั้วต่อเชื่อมต่อส่วนสองด้วยกัน (Part0 → Part1) ขั้วต่อจะเป็น แข็ง หากฟิสิกส์ของ Part1 ถูกล็อคอย่างสมบูรณ์โดย Part0นี้ใช้กับประเภทร่วมต่อไปนี้เท่านั้น:
พารามิเตอร์
ส่งค่ากลับ
GetJoints
คืนข้อต่อหรือข้อจำกัดทั้งหมดที่เชื่อมโยงกับส่วนนี้
ส่งค่ากลับ
ชุดของข้อต่อหรือข้อจํากัดทั้งหมดที่เชื่อมต่อกับชิ้นส่วน
GetMass
รับมวล ส่งคืนค่าของคุณสมบัติอ่านได้เท่านั้น Mass
ฟังก์ชันนี้มีอายุก่อนที่จะเป็นคุณสมบัติมวล ยังคงได้รับการสนับสนุนสำหรับการย้อนกลับไป; คุณควรใช้คุณสมบัติมวลโดยตรง
ส่งค่ากลับ
มวลของชิ้นส่วน
ตัวอย่างโค้ด
This example creates a new part, myPart, in the game's Workspace, with dimensions 4x6x4 studs. The part is also anchored.
Then, myMass is set to equal the mass of the new part. The mass of the part is printed at the end of the print statement:
My part's mass is ...
local myPart = Instance.new("Part")
myPart.Size = Vector3.new(4, 6, 4)
myPart.Anchored = true
myPart.Parent = workspace
local myMass = myPart:GetMass()
print("My part's mass is " .. myMass)
GetNetworkOwner
ส่งคืนผู้เล่นปัจจุบันที่เป็นเจ้าของเครือข่ายส่วนนี้หรือ nil ในกรณีของเซิร์ฟเวอร์
ส่งค่ากลับ
ผู้เล่นปัจจุบันที่เป็นเจ้าของเครือข่ายส่วนนี้หรือ nil ในกรณีของเซิร์ฟเวอร์
GetNetworkOwnershipAuto
ส่งคืนค่าจริงหากเครื่องเกมตัดสินเจ้าของเครือข่ายสำหรับส่วนนี้โดยอัตโนมัติ
ส่งค่ากลับ
ว่าเครื่องเกมตัดสินใจเจ้าของเครือข่ายอัตโนมัติสำหรับส่วนนี้หรือไม่
GetNoCollisionConstraints
ส่งค่ากลับ
GetRootPart
คืนส่วนฐานของการประกอบเมื่อย้ายชิ้นส่วนรวมโดยใช้ CFrameเป็นสิ่งสำคัญที่ต้องย้ายส่วนฐานนี้ (สิ่งนี้จะย้ายส่วนอื่นๆ ทั้งหมดที่เชื่อมโยงกับมันตามลำดับ)ข้อมูลเพิ่มเติมมีอยู่ในบทความ กลุ่มส่วนประกอบ
ฟังก์ชันนี้มีอายุก่อนที่จะเป็นคุณสมบัติ AssemblyRootPart มันยังคงได้รับการสนับสนุนสำหรับการย้อนกลับไปใช้งานได้ แต่คุณควรใช้ AssemblyRootPart โดยตรง
ส่งค่ากลับ
ส่วนฐานของการประกอบ (คอลเลกชันของชิ้นส่วนที่เชื่อมต่อกัน)
GetTouchingParts
คืนตารางของชิ้นส่วนทั้งหมดที่มีการโต้ตอบทางกายภาพกับชิ้นส่วนนี้หากส่วนเองมีการตั้งค่า CanCollide เป็น false แล้วฟังก์ชันนี้จะคืนตารางว่างเปล่า เว้นแต่ส่วนมีวัตถุ TouchInterest ที่เป็นพ่อของมัน (หมายถึงบางอย่างถูกเชื่อมโยงกับอีเวนต์สัมผัสของมัน)ชิ้นส่วนที่อยู่ติดกัน แต่ไม่ซับซ้อนกันไม่ถือว่าสัมผัสฟังก์ชันนี้มีอายุก่อนหน้าฟังก์ชัน WorldRoot:GetPartsInPart() ซึ่งให้ความยืดหยุ่นมากขึ้นและหลีกเลี่ยงกฎพิเศษ TouchInterest ที่อธิบายไว้ข้างต้นใช้ WorldRoot:GetPartsInPart() แทน
ส่งค่ากลับ
ตารางของทุกส่วนที่ซ้อนทับและสามารถชนกับส่วนนี้ได้
GetVelocityAtPosition
คืนความเร็วเชิงเส้นของการประกอบชิ้นส่วนในตำแหน่งที่กำหนดเมื่อเทียบกับชิ้นส่วนนี้สามารถใช้เพื่อระบุความเร็วเชิงเส้นของชิ้นส่วนในการประกอบที่ไม่ใช่ชิ้นส่วนรากหากการประกอบไม่มีความเร็วเฉพาะทาง ความเร็วเชิงเส้นจะเหมือนกันเสมอสำหรับทุกตําแหน่ง
พารามิเตอร์
ส่งค่ากลับ
IsGrounded
ส่งคืนจริงหากวัตถุเชื่อมต่อกับส่วนที่จะถือไว้ในตําแหน่ง (เช่นส่วน Anchored ) มิฉะนั้นจะส่งคืนเป็นเท็จในการประกอบที่มีส่วน Anchored ส่วนอื่น ๆ ทั้งหมดจะถูกกระจาย
ส่งค่ากลับ
ว่าวัตถุจะเชื่อมต่อกับส่วนที่จะรักษาไว้ที่เดิมหรือไม่
Resize
เปลี่ยนขนาดของวัตถุเช่นเดียวกับการใช้เครื่องมือปรับขนาดของ Studio
พารามิเตอร์
ด้านที่จะปรับขนาดใหม่
เท่าใดที่จะเติบโต/หดบนด้านที่ระบุ
ส่งค่ากลับ
ว่าส่วนจะถูกปรับขนาดหรือไม่
SetNetworkOwner
ตั้งผู้เล่นที่กำหนดเป็นเจ้าของเครือข่ายสำหรับส่วนที่เชื่อมต่อทั้งหมดและส่วนนี้เมื่อ playerInstance มี nil เซิร์ฟเวอร์จะเป็นเจ้าของแทนผู้เล่น
พารามิเตอร์
ผู้เล่นได้รับสิทธิ์เป็นเจ้าของเครือข่ายส่วนหนึ่ง
ส่งค่ากลับ
SetNetworkOwnershipAuto
ให้เครื่องเกมตัดสินได้อย่างไดนามิกว่าใครจะจัดการกับฟิสิกส์ของชิ้นส่วน (หนึ่งในไคลเอนต์หรือเซิร์ฟเวอร์)
ส่งค่ากลับ
TorqueToAngularAcceleration
พารามิเตอร์
ส่งค่ากลับ
IntersectAsync
สร้างใหม่ IntersectOperation จากรูปทรงที่ซ้อนทับกันของชิ้นส่วนและชิ้นส่วนอื่นๆ ในรายการที่กำหนดสนับสนุนเฉพาะ Parts เท่านั้น ไม่ใช่ Terrain หรือ MeshPartsคล้ายกับ Clone() วัตถุที่ส่งคืนไม่มีตั้งค่า Parent
คุณสมบัติต่อไปนี้จากส่วนที่เรียกใช้ถูกประยุกต์ให้กับผลลัพธ์ IntersectOperation :
ในการเปรียบเทียบภาพต่อไปนี้ IntersectAsync() จะเรียกใช้บนบล็อกสีม่วงโดยใช้ตารางที่มีบล็อกสีฟ้าผลลัพธ์ IntersectOperation จะแยกออกเป็นรูปร่างของภูมิภาคที่ซ้อนทับกันของทั้งสองส่วน

<figcaption>ชิ้นส่วนแยก</figcaption>

<figcaption>ผลลัพธ์ <code>Class.IntersectOperation</code></figcaption>
บันทึก
- ชิ้นส่วนเดิมยังคงอยู่อย่างสมบูรณ์หลังจากการดำเนินการบรรจบสำเร็จในกรณีส่วนใหญ่คุณควร Destroy() ทั้งหมดของชิ้นส่วนเดิมและเป็นพ่อของส่วนที่ส่งคืน IntersectOperation ไปยังสถานที่เดียวกับที่เรียก BasePart
- โดยค่าสีใบหน้าของการแยกตัวที่ได้รับจะถูกยืมมาจากคุณสมบัติ Color ของส่วนเดิมเพื่อเปลี่ยนทั้งหมดของจุดกบฏเป็นสีเฉพาะ ให้ตั้งค่าคุณสมบัติ UsePartColor ของมันเป็น true
- หากการดำเนินการบรรจบจะทำให้ได้ชิ้นส่วนที่มีมากกว่า 20,000 เหลี่ยม จะถูกเรียบเรียงให้เหลือเพียง 20,000 เหลี่ยม
พารามิเตอร์
วัตถุที่มีส่วนร่วมในจุดแยก
ค่า Enum.CollisionFidelity สำหรับผลลัพธ์ IntersectOperation
ค่า Enum.RenderFidelity ของผลลัพธ์ PartOperation
ส่งค่ากลับ
ผลลัพธ์ IntersectOperation ด้วยชื่อเริ่มต้น สัมผัส .
SubtractAsync
สร้างใหม่ UnionOperation จากส่วนโดยลบเอกภาพที่ใช้โดยชิ้นส่วนในช่องที่กำหนดสนับสนุนเฉพาะ Parts เท่านั้น ไม่ใช่ Terrain หรือ MeshPartsคล้ายกับ Clone() วัตถุที่ส่งคืนไม่มีตั้งค่า Parent
โปรดทราบว่าสหภาพที่ได้รับจะไม่สามารถว่างเปล่าได้เนื่องจากการหักลบ หากการดำเนินการจะทำให้ภาพรวมว่างเปล่าอย่างสมบูรณ์ ก็จะล้มเหลว
ในการเปรียบเทียบภาพต่อไปนี้ SubtractAsync() จะเรียกใช้บนทรงกระบอกสีฟ้าโดยใช้ตารางที่มีบล็อกสีม่วงผลลัพธ์ UnionOperation จะแปลงเป็นรูปที่ลบเอกลักษณ์ของบล็อกจากทรงกระบอก

<figcaption>ชิ้นส่วนแยก</figcaption>

<figcaption>ผลลัพธ์ <code>Class.UnionOperation</code></figcaption>
พารามิเตอร์
วัตถุที่มีส่วนร่วมในการหักลบ
ค่า Enum.CollisionFidelity สำหรับผลลัพธ์ UnionOperation
ค่า Enum.RenderFidelity ของผลลัพธ์ PartOperation
ส่งค่ากลับ
ผลลัพธ์ UnionOperation ด้วยชื่อเริ่มต้น Union .
ตัวอย่างโค้ด
This example demonstrates how to subtract part(s) from another BasePart to form a negated UnionOperation.
local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- Perform subtract operation
local success, newSubtract = pcall(function()
return mainPart:SubtractAsync(otherParts)
end)
-- If operation succeeds, position it at the same location and parent it to the workspace
if success and newSubtract then
newSubtract.Position = mainPart.Position
newSubtract.Parent = Workspace
end
-- Destroy original parts which remain intact after operation
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
end
UnionAsync
สร้างใหม่ UnionOperation จากส่วน, บวกกับรูปร่างที่ใช้โดยชิ้นส่วนในช่วงที่กำหนดสนับสนุนเฉพาะ Parts เท่านั้น ไม่ใช่ Terrain หรือ MeshPartsคล้ายกับ Clone() วัตถุที่ส่งคืนไม่มีตั้งค่า Parent
คุณสมบัติต่อไปนี้จากส่วนที่เรียกใช้ถูกประยุกต์ให้กับผลลัพธ์ UnionOperation :
ในการเปรียบเทียบภาพต่อไปนี้ UnionAsync() จะเรียกใช้บนบล็อกสีฟ้าโดยใช้ตารางที่มีทรงกระบอกสีม่วงผลลัพธ์ UnionOperation จะแยกออกเป็นรูปร่างของภูมิทัศน์รวมของทั้งสองส่วน

<figcaption>ชิ้นส่วนแยก</figcaption>

<figcaption>ผลลัพธ์ <code>Class.UnionOperation</code></figcaption>
บันทึก
- ชิ้นส่วนเดิมยังคงอยู่อย่างสมบูรณ์หลังจากการดำเนินการรวมที่ประสบความสำเร็จในกรณีส่วนใหญ่คุณควร Destroy() ทั้งหมดของชิ้นส่วนเดิมและเป็นพ่อของส่วนที่ส่งคืน UnionOperation ไปยังสถานที่เดียวกับที่เรียก BasePart
- โดยค่าเริ่มต้น สหภาพที่ได้รับจะเคารพคุณสมบัติ Color ของแต่ละส่วนเพื่อเปลี่ยนสหภาพทั้งหมดเป็นสีเฉพาะ ตั้งค่าคุณสมบัติ UsePartColor ของมันเป็น true
- หากการดำเนินการสหภาพจะทำให้ได้ชิ้นส่วนที่มีมากกว่า 20,000 เหลี่ยม จะถูกเรียบเรียงให้เหลือเพียง 20,000 เหลี่ยม
พารามิเตอร์
วัตถุที่มีส่วนร่วมในสหภาพกับส่วนที่เรียกใช้
ค่า Enum.CollisionFidelity สำหรับผลลัพธ์ UnionOperation
ค่า Enum.RenderFidelity ของผลลัพธ์ PartOperation
ส่งค่ากลับ
ผลลัพธ์ UnionOperation ด้วยชื่อเริ่มต้น Union .
ตัวอย่างโค้ด
This example demonstrates how to combine the geometry of one BasePart with the geometry of other part(s) to form a UnionOperation.
local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- Perform union operation
local success, newUnion = pcall(function()
return mainPart:UnionAsync(otherParts)
end)
-- If operation succeeds, position it at the same location and parent it to the workspace
if success and newUnion then
newUnion.Position = mainPart.Position
newUnion.Parent = Workspace
end
-- Destroy original parts which remain intact after operation
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
end
อีเวนต์
TouchEnded
ไฟเมื่อส่วนหยุดสัมผัสส่วนอื่นภายใต้เงื่อนไขที่คล้ายกับของ BasePart.Touched
อีเวนต์นี้ทำงานร่วมกับ Workspace.TouchesUseCollisionGroups เพื่อระบุว่า กลุ่มการชนกัน จะได้รับการยอมรับสำหรับการตรวจจับหรือไม่
พารามิเตอร์
ตัวอย่างโค้ด
This code sample creates a BillboardGui on a part that displays the number of parts presently touching it.
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("Touch started: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("Touch ended: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)
Touched
อีเวนต์ สัมผัส อินสแตนซ์หาก ส่วน A ชนกับ ส่วน B แล้ว PartA.Touched ไฟด้วย ส่วน B และ PartB.Touched ไฟด้วย ส่วน A อีเวนต์นี้จะยิงเฉพาะในผลของการเคลื่อนไหวทางกายภาพเท่านั้น ดังนั้นจะไม่ยิงหากคุณสมบัติ CFrame ถูกเปลี่ยนให้ส่วนหนึ่งซ้อนกับส่วนอื่นนี่ยังหมายความว่าอย่างน้อยหนึ่งในชิ้นส่วนที่เกี่ยวข้องจะต้อง ไม่ ไม่อยู่ในเวลาของการชนกัน
อีเวนต์นี้ทำงานร่วมกับ Workspace.TouchesUseCollisionGroups เพื่อระบุว่า กลุ่มการชนกัน จะได้รับการยอมรับสำหรับการตรวจจับหรือไม่
พารามิเตอร์
ส่วนอื่นที่มาสัมผัสกับส่วนที่กำหนดไว้
ตัวอย่างโค้ด
This code sample creates a BillboardGui on a part that displays the number of parts presently touching it.
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("Touch started: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("Touch ended: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)
This code sample demonstrates how to connect the BasePart.Touched event of multiple parts in a Model to one function.
local model = script.Parent
local function onTouched(otherPart)
-- Ignore instances of the model coming in contact with itself
if otherPart:IsDescendantOf(model) then
return
end
print(model.Name .. " collided with " .. otherPart.Name)
end
for _, child in pairs(model:GetChildren()) do
if child:IsA("BasePart") then
child.Touched:Connect(onTouched)
end
end