How to make a part face the direction of the camera

I’m trying to make a flying broom and currently it does move however the broom doesn’t following the direction of the camera. The broom has a handle that has a ConstraintWeld to the HumanoidRootPart. This is my code so far.

How the broom works:

How the flying works without setting CFrame of the broom handle:

How the flying works with setting CFrame of the broom handle:

connection = flyingEvent.OnServerEvent:Connect(function(player, keyPressed)
	local character = player.Character or player.CharacterAdded
	--local humanoid = character:WaitForChild('Humanoid')
	local camera = game:GetService('Workspace').CurrentCamera
	local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
	local cameraDirection = humanoidRootPart.CFrame:ToObjectSpace(camera.CFrame).LookVector
	
	broom:WaitForChild('Handle').CFrame = CFrame.new(broom:WaitForChild('Handle').Position, Vector3.new(camera.CFrame.LookVector.X * 900000, broom:WaitForChild('Handle').Position.Y, camera.CFrame.LookVector.Z * 900000))

This happens because your camera type is Custom and it always follows the player.

Try changing camera type to scriptable and set CFrame of camera to HumanoidRootPart CFrame with offset

1 Like
broom.Handle.CFrame = CFrame.new(broom.Handle.Position, Vector3.new(broom.Handle.Position.X, camera.CFrame.LookVector.Y, broom.Handle.Position.Z)
1 Like

This seems to do the same thing as not setting the CFrame of the handle.

Wdym with offset? + Vector3.new(0, 5, 0)?

connection = flyingEvent.OnServerEvent:Connect(function(player, keyPressed)
	local character = player.Character or player.CharacterAdded
	--local humanoid = character:WaitForChild('Humanoid')
	--local camera = game:GetService('Workspace').Camera
	local currentCamera = game:GetService('Workspace').CurrentCamera
	local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
	local cameraDirection = humanoidRootPart.CFrame:ToObjectSpace(currentCamera.CFrame).LookVector
	
	currentCamera.CameraType = 'Scriptable'

	--broom:WaitForChild('Handle').CFrame = CFrame.new(broom:WaitForChild('Handle').Position, Vector3.new(currentCamera.CFrame.LookVector.X * 900000, broom:WaitForChild('Handle').Position.Y, currentCamera.CFrame.LookVector.Z * 900000))
	broom:WaitForChild('Handle').CFrame = humanoidRootPart.CFrame 
	--broom.Handle.CFrame = CFrame.new(broom.Handle.Position, Vector3.new(broom.Handle.Position.X, currentCamera.CFrame.LookVector.Y, broom.Handle.Position.Z))
	--broom:WaitForChild('Handle').CFrame = CFrame.new(broom:WaitForChild('Handle').CFrame.Position) * currentCamera.CFrame.Rotation

Yes, CFrame() * CFrame(0, -5, 0)

Like this? CFrame.new()? It seems to be doing something…

broom:WaitForChild('Handle').CFrame = humanoidRootPart.CFrame * CFrame(0, -5, 0)

Yes, but do not use HumanoidRootPart CFrame, but Camera CFrame.

Idk wut I’m doing wrong.

connection = flyingEvent.OnServerEvent:Connect(function(player, keyPressed)
	local character = player.Character or player.CharacterAdded
	--local humanoid = character:WaitForChild('Humanoid')
	--local camera = game:GetService('Workspace').Camera
	local currentCamera = game:GetService('Workspace').CurrentCamera
	local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
	local cameraDirection = humanoidRootPart.CFrame:ToObjectSpace(currentCamera.CFrame).LookVector
	
	currentCamera.CameraType = 'Scriptable'

	--broom:WaitForChild('Handle').CFrame = CFrame.new(broom:WaitForChild('Handle').Position, Vector3.new(currentCamera.CFrame.LookVector.X * 900000, broom:WaitForChild('Handle').Position.Y, currentCamera.CFrame.LookVector.Z * 900000))
	--broom:WaitForChild('Handle').CFrame = humanoidRootPart.CFrame + Vector3.new(0, 5, 0)
	broom:WaitForChild('Handle').CFrame = currentCamera.CFrame * CFrame.new(0, -5, 0)
	--broom.Handle.CFrame = CFrame.new(broom.Handle.Position, Vector3.new(broom.Handle.Position.X, currentCamera.CFrame.LookVector.Y, broom.Handle.Position.Z))
	--broom:WaitForChild('Handle').CFrame = CFrame.new(broom:WaitForChild('Handle').CFrame.Position) * currentCamera.CFrame.Rotation```
2 Likes

Maybe you should try setting HumanoidRootPart rotation to Camera’s? (This sounds dumb)

or set the broom’s CFrame to HumanoidRootPart’s with offset. Camera follows HumanoidRootPart and Broom follows HumanoidRootPart

1 Like

Im dumb sorry try this:

broom.Handle.CFrame = CFrame.new(broom.Handle.Position, Camera.LookVector)

This does this.

connection = flyingEvent.OnServerEvent:Connect(function(player, keyPressed)
	local character = player.Character or player.CharacterAdded
	--local humanoid = character:WaitForChild('Humanoid')
	--local camera = game:GetService('Workspace').Camera
	local currentCamera = game:GetService('Workspace').CurrentCamera
	local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
	local cameraDirection = humanoidRootPart.CFrame:ToObjectSpace(currentCamera.CFrame).LookVector
	
	currentCamera.CameraType = 'Scriptable'

	--broom.Handle.CFrame = CFrame.new(broom.Handle.Position, Vector3.new(currentCamera.CFrame.LookVector.X * 900000, broom.Position.Y, currentCamera.CFrame.LookVector.Z * 900000))
	broom.Handle.CFrame = CFrame.new(broom.Handle.Position, currentCamera.CFrame.LookVector)
	--broom:WaitForChild('Handle').CFrame = humanoidRootPart.CFrame + Vector3.new(0, 5, 0)
	--broom:WaitForChild('Handle').CFrame = currentCamera.CFrame.LookVector + CFrame.new(0, -5, 0)
	--humanoidRootPart.CFrame = currentCamera.CFrame * CFrame.new(0, -5, 0)
	--broom.Handle.CFrame = CFrame.new(broom.Handle.Position, Vector3.new(broom.Handle.Position.X, currentCamera.CFrame.LookVector.Y, broom.Handle.Position.Z))
	--broom:WaitForChild('Handle').CFrame = CFrame.new(broom:WaitForChild('Handle').CFrame.Position) * currentCamera.CFrame.Rotation
	
	if keyPressed == Enum.KeyCode.W then
		character:PivotTo(character:GetPivot() * CFrame.new(0, 0, -1))
	elseif keyPressed == Enum.KeyCode.A then
		character:PivotTo(character:GetPivot() * CFrame.new(-1, 0, 0))
	elseif keyPressed == Enum.KeyCode.S then
		character:PivotTo(character:GetPivot() * CFrame.new(0, 0, 1))
	elseif keyPressed == Enum.KeyCode.D then
		character:PivotTo(character:GetPivot() * CFrame.new(1, 0, 0))
	elseif keyPressed == Enum.KeyCode.Space then
		character:PivotTo(character:GetPivot() * CFrame.new(0, 1, 0))
	elseif keyPressed == Enum.KeyCode.LeftShift then
		-- a
	elseif keyPressed == Enum.KeyCode.LeftControl then
		character:PivotTo(character:GetPivot() * CFrame.new(0, -1, 0))
	end
end)

Umm pretty cool ig? seems like mine is more uhh readable?

oh sorry forgot to send the video.


It doesn’t seem to be move at the cameras direction, it makes the movement rotate instead of going straight or reverse backwards.

1 Like