Why does this not change my position and orientation of the part?

Any help?

local pos = game:GetService("Workspace").CurrentCamera.CFrame.Position
local rot = game.Workspace.CurrentCamera.CFrame:ToEulerAnglesXYZ()

while true do
	wait()
	script.Parent.Rotation = Vector3.new(rot)
	script.Parent.Position = Vector3.new(pos)
end

What exactly are you attempting to accomplish? If you are trying to make a custom player camera, moving the part will not move the camera; Also, I believe it is ‘Orientation’ not ‘Rotation’.

2 Likes

Your first problem is that you read camera position and angle once, at the start of your script. It’s going to read those values in once, then set the position and rotation an infinite number of times after that.

Your second problem is that you’re overcomplicating things a bit. You’ve got a camera CFrame, and you’re trying to split it into position and rotation and apply it separately. You can just set the CFrame of the part, though. This does what I think you wanted to do.

while true do
	wait()
	script.Parent.CFrame = game:GetService("Workspace").CurrentCamera.CFrame
end

This is going to look very stuttery because wait() is more than one frame of game time. If you want it to be smooth, I’d recommend looking into Heartbeat.

Is there a way to offset the part?

the problem with your script is your using the same pos and rot over and over and over again without updating them inside the while true loop


this is how i would make your script

local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera

-- every heartbeat call this function
runService.Heartbeat:Connect(function(deltaTime)
	-- create a vector3 offset using the camera lookvector and move 10 studs forward
	local offset = camera.CFrame.LookVector * 10

	-- position and rotate the script parent to match the camera and move it by the offset
	script.Parent.CFrame = camera.CFrame + offset
end)

Yes. There’s a property called LookVector in CFrames that you can use to do that. Basically, it’s a Vector3 with a length of 1 that points the direction the CFrame is facing. We can add Vector3s and CFrames, so we can just offset it by a Vector3.

If you want to offset the part forward or backward, you can use something like

local camframe = game:GetService("Workspace").CurrentCamera.CFrame
script.Parent.CFrame = camCFrame + camCFrame.LookVector * 10
-- This would offset by 10 "forward", away from the camera.

If you want it to go a different direction, look into RightVector and UpVector, which are also CFrame properties.

did that added some up vector but now the lookvector dosent work

local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera

runService.Heartbeat:Connect(function(deltaTime)
	local camframe = game:GetService("Workspace").CurrentCamera.CFrame
	script.Parent.CFrame = camframe + camframe.LookVector * 7 -- dosent work
	script.Parent.CFrame = camframe + camframe.UpVector * -0.6 -- added
end)

That’s because you’re setting the CFrame twice - once with the LookVector as an offset, and once with an UpVector. Since the UpVector was the last setting, it’s what the value is at the end of the script, so that’s what you see.

You’ll have to add up all your offsets into one long nasty line of code (or, as @5uphi did in their example, make an Offset variable, which is probably a better idea).

That would look something like

local offset = camframe.LookVector * 7 + camframe.UpVector * -0.6
local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera

-- every heartbeat call this function
runService.Heartbeat:Connect(function(deltaTime)
	-- create a vector3 offset using the camera lookvector and move 10 studs forward
	local offset = camera.CFrame.LookVector * 10

	-- move the offset up by 5 studs
	offset += camera.CFrame.UpVector * 5

	-- position and rotate the script parent to match the camera and move it by the offset
	script.Parent.CFrame = camera.CFrame + offset
end)

or

local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera

-- every heartbeat call this function
runService.Heartbeat:Connect(function(deltaTime)
	-- position and rotate the script parent to match the camera
	script.Parent.CFrame = camera.CFrame

	-- move forward by 10 studs
	script.Parent.CFrame += camera.CFrame.LookVector * 10

	-- move up by 5 studs
	script.Parent.CFrame += camera.CFrame.UpVector * 5
end)
1 Like

I have a problem, the movement is still very snapy is there a way to fix that?

local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera

runService.Heartbeat:Connect(function(deltaTime)
	local targetCFrame = camera.CFrame
	targetCFrame += camera.CFrame.LookVector * 10
	targetCFrame += camera.CFrame.UpVector * 5
	-- change 0.1 to adjust how fast you want the part to move
	script.Parent.CFrame = script.Parent.CFrame:Lerp(targetCFrame, 0.1)
end)

That just makes it more snappy. btw this is a first person script so I need it to be pretty smooth

Change 0.1 to 0.001 or 0.01 to adjust how fast it will move