Playing animations in a WorldModel - has anyone gotten it to work?

I’m trying to animate a character within a WorldModel within a ViewportFrame.

It’s not working out of the box with my default animation code that works to play an NPC animation in the Workspace.

I’ve seen a lot of old devforum posts about people complaining it didn’t work, but that was in 2019-2020 and it looks like some patches were made.

Does it work now and if so, what special undocumented thing do I need to do to make it work?

Does anyone have a demo of animating a character in a ViewportFrame without just animating in the Workspace and then cloning the parts and CFrames of all the parts involved into the VPF every frame? Because I know I can make that work, but it’s disgusting.

All the parts seem to be where they need to be and my character is created using game.Players:CreateHumanoidModelFromUserId(261)
image

3 Likes

It absolutely does work! And I use normal animating code to do it. Basic steps:

  1. clone to WorldModel
  2. load animation
  3. play animation
  4. camera in viewportframe

2 Likes

Are those custom animations? R6? R15?

Trying to narrow down where the problem could be.

3 Likes

Yes, these are custom animations, skinned meshes.

Did you write your own code? That animate script will get in the way.

1 Like

I got it to work, but not sure what I changed to make it so.

The animation/animator thing could throw some warnings when it encounters a problem that prevents an animation from being played.

If anyone else encounters this issue, here is some code that works.



viewportFrame = script.Parent



--local part = Instance.new("Part")
--part.Material = Enum.Material.Concrete
--part.Color = Color3.new(0.25, 0.75, 1)
--part.Position = Vector3.new(0, 0, 0)
--part.Parent = viewportFrame


local wm = Instance.new("WorldModel")

local char = game.Players:CreateHumanoidModelFromUserId(261)

--local char = game.ReplicatedStorage["Wsly"]:Clone()
local part = nil

print(char)
char.PrimaryPart = char.HumanoidRootPart
char:SetPrimaryPartCFrame(CFrame.new(0,0,0))
char.Parent = wm

local part = char.PrimaryPart

wm.Parent = viewportFrame




local viewportCamera = Instance.new("Camera")
viewportFrame.CurrentCamera = viewportCamera
viewportCamera.Parent = viewportFrame

viewportCamera.CFrame = CFrame.new(Vector3.new(0, 2, 12), part.Position)



local Anim = Instance.new("Animation")
Anim.AnimationId = "http://www.roblox.com/asset/?id=507777826" -- walk





local a = char.Humanoid:LoadAnimation(Anim)
a.Looped = true
a.Priority = Enum.AnimationPriority.Action
a:Play()


local runService = game:GetService("RunService") -- get runservice before the loop
local startingTick = tick()



-- rotating mesh
while true do -- never ending loop
	local diff = tick() - startingTick -- time since loop started


	--viewportCamera.CFrame = CFrame.new(Vector3.new(0, 2, 12), part.Position) * CFrame.fromEulerAnglesXYZ(0,math.rad(diff),0)

	-- Say we have a variable, that's the primary part of the Sword I'll call it Sword.
	local cframe = part.CFrame * CFrame.Angles(0, math.rad(diff) * 100, 0) * CFrame.new(0, 0, -5) 
	--  Change the angles accordingly, and the -5 should be distance from the objects center outwards.
	-- Once you have that, you want to take that position and have it face the position of the sword, and then set the Viewport's camera cframe to that new cframe.
	cframe = CFrame.new(cframe.p, part.Position)
	viewportCamera.CFrame = cframe;

	-- part.CFrame = CFrame.Angles(0, 2*math.rad(diff), 0) <-- two times as fast
	
	
	
	
	-- wait a frame
	runService.RenderStepped:Wait()
end
7 Likes

First, tell me the animate script is dead! That thing mangles your manual idle animations because it’s trying to do it too.

Second, make sure your priorities are correctly saved for all your animations. Walking is Movement, idle is Idle. Just use the same script for both, play the idle animation and forget about it. Walking will override the idle animation, and when you stop, the idle animation will continue to run. A happy code is an efficient code! :slight_smile:

image

Is this a local script or a server script?

1 Like