Want to stay connected to the Scripting Helpers community and get real-time help? Join the Scripting Helpers Discord Server!
Ad
Log in to vote
0

I'm having trouble manipulting the camera with lookVector. [?]

Asked by 1 year ago

Hello. I have been trying to make the players camera constantly face a part. It works fine like this

local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local chr = workspace:WaitForChild(plr.Name)

game:GetService("RunService").RenderStepped:connect(function()
    cam.CFrame = CFrame.new((chr.Head.CFrame).p, workspace.Part.Position)
end)

But when I add .lookVector like so, it completely goes haywire.

local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local chr = workspace:WaitForChild(plr.Name)

game:GetService("RunService").RenderStepped:connect(function()
    cam.CFrame = CFrame.new(chr.Head.CFrame.lookVector * -5, workspace.Part.Position)
end)

Could anyone help?

0
if there's a way that works fine, why dont you use it? loulou1112 35 — 1y

2 answers

Log in to vote
0
Answered by 1 year ago

The first 3 values of CFrame is the position, and the rest is the rotation matrix. You got it mixed up, so instead of:

local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local chr = workspace:WaitForChild(plr.Name)

game:GetService("RunService").RenderStepped:connect(function()
    cam.CFrame = CFrame.new(chr.Head.CFrame.lookVector * -5, workspace.Part.Position)
end)

do:

local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local chr = workspace:WaitForChild(plr.Name)

game:GetService("RunService").RenderStepped:connect(function()
    cam.CFrame = CFrame.new(workspace.Part.Position,chr.Head.CFrame.lookVector * -5)
end)
0
sorry but that didnt work. The way this CFrame.new works is CFrame.new(Vector3 pos, Vector3 lookAt) connor12260311 373 — 1y
0
This is what you're looking for then: cam.CFrame = CFrame.new(chr.Head.Position,workspace.Part.Position) killerbot29003 116 — 1y
Ad
Log in to vote
0
Answered by
Firefaul 129
1 year ago

Another way of solving this is by setting the position of the camera, then the position to look at.

cam.CFrame = CFrame.new(CFrame.new(chr.Head.CFrame * CFrame.new(0,0,-5)).p, chr.Head.Position)

Answer this question