如何将多个模型附加到 Garry's Mod 中的一个实体?

How can I attach multiple models to an entity in Garry's Mod?

我想制作一个像这张图左边的脚本实体,将几个模型组合在一起:设备架、电脑屏幕、键盘、电池等

我想使用 Lua 脚本来完成此操作,而不是使用 Blender 将所有这些模型合并为一个。

我该怎么做?

如果您有两个不同的模型

  • models/machine/machime.mdl
  • models/rack/rack.mdl

然后您可以创建两个实体 :

  1. 机器实体

    1.1 lua/entities/machine/init.lua

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

function ENT:Initialize()
   self:SetModel("models/machime/machime.mdl")
   self:PhysicsInit(SOLID_VPHYSICS)
   self:SetMoveType(MOVETYPE_VPHYSICS)
   self:SetSolid(SOLID_VPHYSICS)

   local phys = self:GetPhysicsObject()

   if phys:IsValid() then
      phys:Wake()
   end
end

1.2 lua/entities/machine/cl_init.lua

include('shared.lua')

function ENT:Draw()
  self:DrawModel()
end

1.3 lua/entities/machine/shared.lua

ENT.Type = "anim"
ENT.Base = "base_gmodentity"

ENT.AdminOnly   = false
ENT.Spawnable   = true
ENT.PrintName   = "My machine"
ENT.Purpose     = "This is a really good machine"
  1. 机架实体

    2.1 lua/entities/rack/init.lua

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

function ENT:Initialize()
   self:SetModel("models/rack/rack.mdl")
   self:PhysicsInit(SOLID_VPHYSICS)
   self:SetMoveType(MOVETYPE_VPHYSICS)
   self:SetSolid(SOLID_VPHYSICS)

   local phys = self:GetPhysicsObject()

   if phys:IsValid() then
      phys:Wake()
   end
end

2.2 lua/entities/rack/cl_init.lua

include('shared.lua')

function ENT:Draw()
  self:DrawModel()
end

2.3 lua/entities/rack/shared.lua

ENT.Type = "anim"
ENT.Base = "base_gmodentity"

ENT.AdminOnly   = false
ENT.Spawnable   = true
ENT.PrintName   = "My rack"
ENT.Purpose     = "To rack them all"

终于把架子放进机器了可以用ENT:SetParent:

local machine = ents.Create("machine") -- create a new machine entity
machine:SetPos(THE_POSITION) -- change the position of spawn
machine:Spawn() -- spawn it

local rack = ents.Create("rack") -- create a rack entity
rack:SetParent(machine) -- set the parent to machine
rack:SetLocalPos(THE_LOCAL_POS) -- where the rack is relative to the machine
rack:Spawn() -- spawn it

注意:在最后一部分示例中生成将机架放入机器中。您还可以使用 ShouldCollide 挂钩在它们即将碰撞时将机架放入机器中!