How To Make The Camera Roll With Part

I am trying to make the camera roll with the aircraft but cannot for the life of me wrap my head around as to why the rotations are so off when the aircraft starts to roll.

Here is a video as to what is happening

Below is the code I use for the camera function

local plane = game.Workspace:WaitForChild("Plane")
local currCamera = game.Workspace.CurrentCamera

local function cameraMovement()
	currCamera.CameraType = "Scriptable"
	currCamera.CFrame = CFrame.new(plane.CFrame.Position + plane.CFrame.LookVector * 80, plane.Position)  * plane.CFrame.Rotation:Inverse()
end

game:GetService("RunService").RenderStepped:Connect(cameraMovement)

I just want the camera to roll and follow the aircraft.

If anyone has any advice or could point me in the right direction to fixing this would be greatly appreciated!

Have you tried using CFrame.lookAt()?

1 Like

Instead of the plane.CFrame.Rotation:Inverse()?

No, I mean this.

CFrame.lookAt(plane.CFrame.Position + plane.CFrame.LookVector * 80, plane.Position)  * plane.CFrame.Rotation:Inverse()

I just gave it a go and the same thing happens when it rolls. I see it has an up vector as a third parameter I will try using that

local plane = game.Workspace:WaitForChild("Plane")
local currCamera = game.Workspace.CurrentCamera

local function cameraMovement()
	currCamera.CameraType = "Scriptable"

	local cameraPos = plane.Position + plane.CFrame.LookVector * 80 + plane.CFrame.RightVector * -20

	local cameraRot = plane.CFrame.Rotation:ToEulerAnglesXYZ()
	cameraRot = Vector3.new(0, cameraRot.Y, cameraRot.Z)

	currCamera.CFrame = CFrame.new(cameraPos, plane.Position) * CFrame.fromEulerAnglesXYZ(cameraRot)
end

game:GetService("RunService").RenderStepped:Connect(cameraMovement)

Can you try this and tell me if this work ?

That’s not how :ToEulerAnglesXYZ() works by the way.

local x,y,z = plane.CFrame.Rotation:ToEulerAnglesXYZ()

It errored out at this line
image

Change that line and the one above it to this.

local x,y,z = plane.CFrame.Rotation:ToEulerAnglesXYZ()
cameraRot = Vector3.new(0,y,z)

Ah you are brilliant! I used the third param of CFrame.LookAt() and just placed the plane’s up vector:

currCamera.CFrame = CFrame.lookAt(plane.CFrame.Position + plane.CFrame.LookVector * 80, plane.Position, plane.CFrame.UpVector)

I was completely unaware of this method. Thank you!

1 Like

Then your problem is solved with this solution ?

Then it need to be like this ?

local plane = game.Workspace:WaitForChild("Plane")
local currCamera = game.Workspace.CurrentCamera

local function cameraMovement()
	currCamera.CameraType = "Scriptable"

	local cameraPos = plane.Position + plane.CFrame.LookVector * 80 + plane.CFrame.RightVector * -20

	local x, y, z = plane.CFrame.Rotation:ToEulerAnglesXYZ()
	local cameraRot = Vector3.new(0, y, z)

	currCamera.CFrame = CFrame.new(cameraPos, plane.Position) * CFrame.fromEulerAnglesXYZ(cameraRot)
end

game:GetService("RunService").RenderStepped:Connect(cameraMovement)

I just used the CFrame.LookAt() function as it changes the upvector of the camera as well which was causing me issues. Thanks for all your help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.