How to get directions camera Is looking at



There is a similarity between the images, and that is that the bottom of the pictures points at the objective back and the top to the front, However, I can’t get that data with look / up vectors, if i used that there would be 1 case where according to the vector it is looking straight down, crossing the vectors does not work either as the same happens when you look diagonally downward.
How could I make it so It’s a constant value that gives the direction of the top,right,front of the screen but doesn’t point downward.

You could just stick with the lookVector and check if the look vector is straight down or not and if it is straight down just use the upVector to tell which way is forward.

You could use the LookVector, store the last LookVector, then if the LookVector is straight down it uses the last LookVector? (I assume you’re connecting to RenderStepped or something similar)

I’m I little confused as to what you’re asking, maybe try rewording this sentence or drawing a picture?

I am trying to get a vector3 that always points forward relative to the camera, like this

![Untitled1|690x388]

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

local camera = Workspace.CurrentCamera

local function getDirectionVector()
    local lookVector = camera.CFrame.LookVector
    local lookVectorX = lookVector.X
    local lookVectorZ = lookVector.Z
    if (lookVectorX == lookVectorZ) and ((lookVectorX == 0) or (lookVectorX == 1)) then --To handle looking downwards edge case
        lookVectorX = upVector.X
        lookVectorZ = upVector.Z
    end
    return Vector3.new(lookVector.X, 0, lookVector.Z).Unit
end


local currentDirectionVector = getDirectionVector()
local renderConnection = RunService.RenderStepped:Connect(function() --RenderStepped connection to update, could use PropertyChangedSignal or something slower like Heartbeat
    currentDirectionVector = getDirectionVector()
end)

Also, I’m pretty sure the default camera scripts don’t let the camera face directly downwards or upwards to avoid this type of edge case (I’d double check that, I’m not certain).

6 Likes

Aaand i just realised humanoid.Movedirection is a thing very sorry for wasting your time :neutral_face:
Nevertheless that was a pretty cool solution, keep it up :+1:

1 Like