I am trying to create a viewmodel for my FPS game. I've just started and already ran into a problem.
I am trying to create two copies of the roblox character's arms, but if the player's torso is rotated in any way, the cloned parts appear in world space, and not in object space (in relation to the torso's current position and rotation)
How do I fix this? I've read up on CFrame:toObjectSpace(), but I am unsure about its usage.
Here is my code:
local character = game.Workspace.Dummy local fakearms = {} local welds = {} local gun = game.ServerStorage.g36 local rarm = Instance.new("Part") local larm = Instance.new("Part") rarm.Size = Vector3.new(1,2,1) rarm.BrickColor = BrickColor.new("Pastel Orange") larm.Size = Vector3.new(1,2,1) larm.BrickColor = BrickColor.new("Pastel Orange") leftweld = Instance.new("Weld",character.HumanoidRootPart) rightweld = Instance.new("Weld",character.HumanoidRootPart) welds[1] = rightweld welds[2] = leftweld welds[1].Part0 = character.HumanoidRootPart welds[1].Part1 = rarm welds[2].Part0 = character.HumanoidRootPart welds[2].Part1 = larm rarm.Parent = character larm.Parent = character rarm.CFrame = CFrame.new(character.Torso.CFrame.p) * CFrame.new(1.5,0,0) larm.CFrame = CFrame.new(character.Torso.CFrame.p) * CFrame.new(-1.5,0,0)
I will explain how to use CFrame:toWorldSpace().
The syntax for the function is as follows:
OriginalCFrame:toWorldSpace(TranslateByThisCFrame)
OriginalCFrame
in this case would be Torso.CFrame
.
TranslateByThisCFrame
would be either CFrame.new(1.5,0,0)
or CFrame.new(-1.5,0,0)
, depending on the arm being translated.
Here is an example of the usage. This translates Left Arm's position -1.5 studs in the x-axis from HumanoidRootPart's position in local space.
local larm = workspace.Player1["Left Arm"]:Clone() larm.CFrame = workspace.Player1.HumanoidRootPart.CFrame:toWorldSpace(CFrame.new(-1.5,0,0)) larm.Parent = workspace
The same can be done for the right arm, but with a positive 1.5 instead.