How to make a viewport frame character rotation

Hello,

So I basically want my character to be rotated around and around in the viewportframe but right now it only rotates the humanoidrootpart and it has a lot of flickering here is my script

local rotation = 0
local cam = Instance.new("Camera",script.Parent)
cam.CFrame = CFrame.new(8,0,0)
game:GetService("RunService").RenderStepped:Connect(function()
	if rotation>=360 then
		rotation = 0
	else
		rotation = rotation+1
	end
	if game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart") then
		game.Players.LocalPlayer.Character.Archivable = true
		local e = game.Players.LocalPlayer.Character:Clone()
		game.Players.LocalPlayer.Character.Archivable = false
		e.Parent = script.Parent
		e.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
		e.HumanoidRootPart.CFrame = CFrame.new(0,0,0)*CFrame.fromEulerAnglesXYZ(0, math.rad(rotation), 0)
		wait()
		e:Destroy()
	end
end)

- Br, iSyriux

1 Like

You can use SetPrimaryPartCFrame from the model (character). HumanoidRootPart is the primary part.

local rotation = math.rad(1)

runService.RenderStepped:Connect(function()
   local newCFrame = character.PrimaryPart.CFrame:ToWorldSpace(CFrame.Angles(0, rotation, 0))
   character:SetPrimaryPartCFrame(newCFrame)
end)

Also, I think it is better if you create the variables outside render stepped scope, because they are being declared multiple times with the same value.

(I’msorry about my english, it is not my main language :slight_smile: )

Hello,

Thank you for your brilliant insight, @GAFFAL_1236.
It seems to be working, however multiple clones exist at the same time and I don’t want that. I want one clone every renderstep and after the renderstep passes the previous clone is destroyed.

1 Like

Alright, check it out:

local player = game.Players.LocalPlayer
local realCharacter = player.Character or player.CharacterAdded:Wait()

local currentCFrame = realCharacter.PrimaryPart.CFrame  -- Stores last character's CFrame
local rotationSpeed = math.rad(1)


runService.RenderStepped:Connect(function()
   -- Removing old character
   local oldCharacter = viewportFrame:FindFirstChild(realCharacter.Name)
   if oldCharacter then
      oldCharacter:Destroy()
   end

   -- Creating new character
   local newCharacter = realCharacter:Clone()
   newCharacter.Parent = viewportFrame

   -- Updating CFrame
   local newCFrame = currentCFrame:ToWorldSpace(0, rotationSpeed, 0)
   newCharacter:SetPrimaryPartCFrame(newCFrame)

   currentCFrame = newCFrame
end)

I followed these steps:

  1. Remove old character
  2. Create new character
  3. Update its CFrame based at the last one

It should work (let me know if it doesn’t), however, I think it could be a bit laggy since you’re creating a new character for each frame. Maybe you should listen for changes of real character and replicate it at the rendered character. Something like this:

local player = game.Players.LocalPlayer
local realCharacter = player.Character or player.CharacterAdded:Wait()

local rotationSpeed = math.rad(1)

local renderedCharacter = realCharacter:Clone()
renderedCharacter.Parent = viewportFrame


-- Listening for changes
for _, realObject in ipairs(realCharacter:GetDescendants()) do
   -- Finding the object at rendered character
   local renderedObject = renderedCharacter:FindFirstChild(realObject.Name, true)  -- works like "FindFirstDescendant"

   -- Updating object at rendered character
   realObject.Changed:Connect(function(property)
      renderedObject[property] = realObject[property]
   end)
end


-- Rotating
runService.RenderStepped:Connect(function()
   local newCFrame = renderedCharacter.PrimaryPart:ToWorldSpace(0, rotationSpeed, 0)
   renderedCharacter:SetPrimaryPartCFrame(newCFrame)
end)

(It only works if every single object at the character has a single name)

Again, sorry about my english :slight_smile:

Hello,

Unfortunately, your method proves to be ineffective, and now it results in the cloned character not being deleted at all. Your second method is inapplicable to my system, because I will be adding hats and other instances to the real character, thus I will need to clone it every frame.

1 Like

Hello,

It works.

3 Likes

Ok, about that, think you can also listen to objects added in your real character and replicate it for the rendered character.

Can you please check if the script found the character, like:

local oldCharacter = viewportFrame:FindFirstChild(realCharacter.Name)
  if oldCharacter then
    print('Found')
     oldCharacter:Destroy()
  else
     print('Not found')
  end
1 Like

Well done! Glad it works! Never mind about my reply then :slight_smile:

1 Like