How to make an object rotate depending on camera angle

Hi, I’m trying to make a controllable missile for fun. I can’t seem to get it to rotate it the correct way. My goal is to make it rotate a set amount depending on which way the camera is facing. You should be able to go Up, down, left or right.

My problem is that the missile isn’t rotating 90 degrees to the right.

this is the test script (only focus on rotating part).

local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")

local rotation = CFrame.identity

local part = script:WaitForChild("BallPart"):Clone()
part.Parent = workspace


-- just waiting for the character to do their camera manipulations before we do ours
-- not the best way ik
task.wait(2)

local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Track
camera.CameraSubject = part

local linearVel = Instance.new("LinearVelocity")
linearVel.RelativeTo = Enum.ActuatorRelativeTo.World
linearVel.VectorVelocity = Vector3.zero
linearVel.MaxForce = math.huge
linearVel.Attachment0 = part:FindFirstChildOfClass("Attachment")
linearVel.Parent = part


-- bind with priority so we don't have to disable the controls
ContextActionService:BindActionAtPriority("Forward", function(actionName, inputState, inputObject)
	RunService.RenderStepped:Connect(function()
		-- go to the direction the camera is facing
		linearVel.VectorVelocity = camera.CFrame.LookVector * 100
		
		-- cframe.lookat so it looks in the direction the camera is faced
		-- cframe.angles to change the face that is looks in the direction of the camera
		-- rotation for turning in cframe ofc
		part.AlignOrientation.CFrame = CFrame.lookAt(part.Position, part.Position + camera.CFrame.LookVector) * CFrame.Angles(math.rad(90), 0, math.rad(90)) * rotation
	end)
end, false, Enum.ContextActionPriority.High.Value, Enum.KeyCode.W)

ContextActionService:BindActionAtPriority("Right", function(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.End then
		return
	end
	
	-- idk how to do this correctly
	rotation = part.AlignOrientation.CFrame:ToWorldSpace(CFrame.Angles(0, 0, math.rad(90)))
	
end, false, Enum.ContextActionPriority.High.Value, Enum.KeyCode.A)

Any feedback is appreciated!

1 Like