Jump to content

[lua] model movement with attached model like hat


Go to solution Solved by burgelkat,

Recommended Posts

Posted

    -- Hut an Kopf attachen
    if self.hat then
        local head = self.NPC.skeleton:FindBone("mixamorig:Head") or self.NPC.skeleton:FindBone("mixamo:Head") or self.NPC.skeleton:FindBone("Head")
        if head then
            self.hat:SetPosition(Vec3(0,0, -0.06), true)
            self.hat:SetRotation(55,0,0,true)
            self.hat:Attach(self.NPC, head)
            Print("Hut attachiert")
        else
            Print("Head-Bone nicht gefunden")
        end
    end



-- Fuß-Offset berechnen
    local box = self.NPC:GetBounds(BOUNDS_LOCAL)
    self.footOffset = -box.min.y

Bodencheck
    local pickInfo = model:GetWorld():Pick(agentPos + Vec3(0,1,0), agentPos + Vec3(0,-10,0), 0.0, true)
    local npcPos = pickInfo and Vec3(agentPos.x, pickInfo.position.y + self.footOffset, agentPos.z) or Vec3(agentPos.x, agentPos.y + self.footOffset, agentPos.z)
    model:SetPosition(npcPos)

 I have this in my NPC script. With that my npc walk always at ground  and not hover even the ground is not flat.  If i attach a hat to the NPC model the hat would not follow the model. The hat is attached at bone. 

Is it possible that an attached model as a child is not being updated or repositioned accordingly? At first the hat is positioned correctly, but when the NPC moves, the hat floats independently, sometimes above and sometimes below the NPC. Can someone give me a hint, where i should pay attention in the code if i work with attached models like hat or sword, if i work with that code?

Quote

 

  • Like 1
  • 2 weeks later...
  • Solution
Posted

Ok, after a few attempts, I realized that it’s better not to work with Pick, especially in Update. That only works if no additional models (like a hat, sword, etc.) are attached. Therefore, only use 'footOffset' once in Start. Then attaching and moving additional models (hat, sword, etc.) works perfectly. Also the NPC Model is at ground and not hoover anymore. 

 

   -- Beim Start() den Fußpunkt-Offset einmalig setzen
    self.footOffset = -0.2
 
    -- NavAgent erstellen und an NPC anhängen
    if navmesh then 
        self.NavAgent = CreateNavAgent(navmesh)
        local agentPos = self.NavAgent:GetPosition(true)
        self.NPC:SetRotation(0,-180,0)
        self.NavAgent:SetRotation(self.entity:GetRotation(true).y)
        self.NPC:SetPosition(0, self.footOffset, 0)
        self.NPC:Attach(self.NavAgent)

        if self.NavAgent then
            Print("start mit NavAgent")
        end
        if not navmesh then 
            Print("kein NavAgent gestartet")
        end
        self.NavAgent:SetPosition(self.startPosition)
        -- Hier die Rotation explizit auf die Start-Rotation setzen
        self.NPC:SetRotation(self.startRotation) 
    end

 

Screenshot 2025-10-06 185936.png

Screenshot 2025-10-06 190113.png

  • Like 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...