Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to set a camera cframes position and rotation for first person?

Asked by 3 years ago
Edited 3 years ago

Im trying to make a first person game but i need a custom camera. How can i set the cframe of the camera to the position of the player while letting it rotate like a normal first person camera?

local lookAt1

function updatecam()
    local delta = uis:GetMouseDelta()
    local x = delta.X
    local y = delta.Y
    local pos = CFrame.new(character.HumanoidRootPart.Position)
    if Mouse2Hold == true then
        lookAt1 = CFrame.Angles(math.rad(-y),math.rad(0),math.rad(-x))
        cam.CFrame = cam.CFrame * lookAt1 * pos --this is where i need help
    else
        lookAt1 = CFrame.Angles(math.rad(-y),math.rad(-x),math.rad(0))
        cam.CFrame = cam.CFrame * lookAt1 * pos --this is where i need help
    end
end

The issues that i need help with are due to needing to set the cframe in one line as far as i know. This means that if i remove the

cam.CFrame = lookAt1 * pos

then the position of the camera would work fine, but the rotation messes up, due to the rotation setting back to 0 after every frame and not maintaining its previous values. However if i keep it as it was above then the rotation works perfectly but the camera flies backwards, due to it keeping its previous position value while still adding the players current location on to it. For example if the players X is at 10 then it would just keep adding that on to itself and putting the camera at that new coordinate (the player never actually moves), meaning it would be 10, then 20, then 30, and so on every frame.

That was long winded but its the best I can explain it. TLDR I need cam.CFrame * lookAt1 but i cant also have it multiplying the pos by cam.CFrame

0
you can save yourself some trouble by setting StarterPlayer.CameraMode to LockFirstPerson... spunargar -6 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
local player = game.Players.LocalPlayer

player.CameraMode = Enum.CameraMode.LockFirstPerson
Ad

Answer this question