How can I play an animation on an NPC inside of a ViewportFrame?

I have an NPC inside of a ViewportFrame that I want to animate when you interact with it. The NPC is not anchored, and I’ve made sure my script works by using prints and I receive no errors. I’m using Humanoid:LoadAnimation(), and the animation does work on the NPC if I move it out of the ViewportFrame and into the Workspace.

5 Likes

Play the Animation on the npc, then constantly update the camera to the npc (RenderStepped loop) to view the animation

1 Like

What do you mean constantly update the camera? Do I actually have to keep changing the value of something or can I just do Camera.CFrame = Camera.CFrame?

edit: Doing that or actually changing a value of the camera doesn’t play the animation. I can do anything, like moving a part or changing the color of something inside the ViewportFrame but I can’t play an animation

Can no one help me? No one’s helping me on Scripting Helpers either.

I think DatabaseReplace meant you play the animation and use a script to track the positions and use that the modify the Viewport contents.

Physics and animations do not work in viewports. You have to have a copy of the humanoid in the viewport and a copy in workspace. Play the animation on the copy in workspace.

Iterate over each part of the animated humanoid in workspace. Copy the CFrame of each part to the respective part inside the viewport. This is an expensive way to simulate animations.

3 Likes

You can use something like this (sorry for late response)

local thePlayer = game.Players.LocalPlayer --Get the player
local Character = thePlayer.Character or thePlayer.CharacterAdded:Wait()
Character.Archivable = true --Make sure we can clone the character
local newCamera = Instance.new('Camera') --Make a new camera
newCamera.CameraType = Enum.CameraType.Scriptable
local _, err = pcall(function()
	game:GetService("RunService").Heartbeat:Connect(function()
		if thePlayer.Character:FindFirstChild("HumanoidRootPart") then
			for i, descendant in ipairs(script.Parent:GetDescendants()) do --Get all the parts already in the viewport...
				if descendant.ClassName ~= "LocalScript" then -- ... check if it's not the LocalScript ...
					descendant:Destroy() -- ... And destroy it, so we can put the new stuff in.
				end
			end
	
	--Finally, we set the camera to our desired position
			newCamera.CoordinateFrame = CFrame.new((thePlayer.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-5)).p, thePlayer.Character.HumanoidRootPart.Position)
			local me = thePlayer.Character:Clone() --And clone your character into the viewport.otherwise you get an error)
			me.Parent = script.Parent
		end
	end)
end)
if err then end
3 Likes

This worked for me, thank you! Ive been looking for a solution for a long time

1 Like