Hello i'm new to scripting. Was trying to make a script that would make the player face the camera's look vector. Found a lot of examples online but this one was the most simplest for me. however i'm still confused. Was wondering about the difference between those two lines below? why did "root,CFrame.P" get assigned the second time inside the vector in example 1? Most importantly why is example 2 incorrect? Thank u
----Example 1 (correct one)
local cam = workspace.CurrentCamera local plr = game:GetService("Players").LocalPlayer local root = plr.Character.HumanoidRootPart game:GetService("RunService").RenderStepped:connect(function() if plr.Character then root.CFrame = CFrame.new(root.CFrame.p,root.CFrame.p+Vector3.new(cam.CFrame.lookVector.X,0,cam.CFrame.lookVector.Z)) end end)
----Example 2 (Wrong one)
local cam = workspace.CurrentCamera local plr = game:GetService("Players").LocalPlayer local root = plr.Character.HumanoidRootPart game:GetService("RunService").RenderStepped:connect(function() if plr.Character then root.CFrame = CFrame.new(root.CFrame.p, Vector3.new(cam.CFrame.lookVector.X,0,cam.CFrame.lookVector.Z)) end end)
The lookVector returns the unit of the CFrame's facing direction relative to the world. In other words, it will return the return the position 1 stud away from Vector3.new(0, 0, 0) in the direction the CFrame is looking.
So if the Player is looking East, the lookVector will be Vector3.new(1, 0, 0)
When you define your CFrame in your script, you are using:
CFrame.new(Position, DirectionToLook)
DirectionToLook is relative to the Player, whereas a lookVector is relative to the world, so when you only put the lookVector as the DirectionToLook in then you are telling the new CFrame to look in the direction of Vector3.new(1, 0, 0) which would be towards the centre of the map. In the correct example, the lookVector is added to the position vector so it is looking 1 unit away from the HumanoidRootPart in the direction the HumanoidRootPart is facing.
Sorry for the poor explanation, it's a difficult one to get your head around without visual examples.
Hello . To answer my own question, Yes it would fix problem number 2 if i were to do just that. Here is the script:
local cam = workspace.CurrentCamera local plr = game:GetService("Players").LocalPlayer local root = plr.Character.HumanoidRootPart game:GetService("RunService").RenderStepped:connect(function() if plr.Character then local lol = root.CFrame:ToObjectSpace(cam.CFrame) root.CFrame = root.CFrame * CFrame.Angles(0, -lol.LookVector.X, 0) end end)