I have made a small widget that displays the direction of the next target for the player. It works by placing a model in front of current camera and rotating it to point in the direction of some part in the scene that represents the next target. It is kind of neat and it aaalmost works!
The code looks like this;
local modelTemplate:Model = script.Parent:FindFirstChild("InFront") or workspace:FindFirstChild("InFront")
local model:Model = modelTemplate and modelTemplate:Clone()
if model then
model.Parent = workspace
local camera:Camera = workspace.CurrentCamera
local bindingName = model.Name.."RenderStep"
local targetPoint = workspace.SpireTip.Position
game:GetService("RunService"):BindToRenderStep(bindingName, Enum.RenderPriority.Camera.Value + 1, function()
local pointRelative = camera.CFrame:PointToObjectSpace(targetPoint)
local yaw = -math.atan2(pointRelative.Z, pointRelative.X)
local pitch = -math.atan2(pointRelative.Z, pointRelative.Y)
print(string.format("yaw: %0.2f, pitch: %0.2f", yaw, pitch) )
model:PivotTo(camera.CFrame * CFrame.new(0, 3, -8) *CFrame.Angles(0, yaw, 0) )
-- I WANT TO USE PITCH AS WELL BUT IT LOOKS COMPLETELY CRAP:
--model:PivotTo(camera.CFrame * CFrame.new(0, 3, -8) *CFrame.Angles(pitch, 0, 0)*CFrame.Angles(0, yaw, 0) )
end)
end
As you can see in this video, it looks fairly ok when the camera is only rotating around Y axis (yaw only):
robloxapp-20221216-0426548.wmv (1.3 MB)
But as soon as the camera rotates around X axis (pitch) as well, it starts to look really funny. I have tried all manner of changes, switching around Z and Y, switching order of CFrame multiplications, combining both into one CFrame.Angles call etc but it the closest I get is with the commented out line which makes it look like the millenial falcon avoiding astroids. See this video:
robloxapp-20221216-0435148.wmv (1.6 MB)
So, what kind of brain fart are we looking at here. What did I miss?