As @ShadowOfCrimson said, a better way to achieve this is buy using the camera’s CFrame’s lookVector, the lookVector is the direction of looking of that specific cframe. This is the second paramater of CFrame.new(pos, look)
. If we set the “look” paramater of the cframe of part A, to the position of part B, part A would be facing part B.
We won’t actually be setting the character’s humanoidrootpart to the camera’s lookvector (Camera.CFrame.lookVector
), since the lookVector is a unit vector, which means its 1 stud long, so if we do that the player would be looking backwards, because he’s trying to look at the camera’s lookvector which is facing his direction, but its too short so its behind him.
So what we have to do is make it longer, by multiplying it by a scalar (a number).
character:SetPrimaryPartCFrame(CFrame.new(character.HumanoidRootPart.Position, camera.lookVector * 10))
--we set the hrp to the same position and facing where the camera is facing, 10 was just an example, what we multiply by doesnt really matter since how long it is doesn't change the direction
And also your script wasn’t working because of the way you were using that targetPos
variable, you were saving the CFrame property of hrp (rootPart.CFrame
) to that var, and saving a property inside of a variable doesn’t save a reference to that property it saves the value that property was set to, so the rootpart’s cframe won’t change.
Another small suggestion is using :BindToRenderStep()
instead of the RenderStepped
event, they do the same thing, but with the :BindToRenderStep()
method we can control the priority
of the rendering, so in our case we want our thing to have the priority of the camera, makes sense.
Look up :BindToRenderStep()
it’ really useful.
local function UpdateCFrame(char)
char:SetPrimaryPartCFrame(CFrame.new(char.HumanoidRooPart.Position, camera.CFrame.lookVector * 10))
end
RunService:BindToRenderStep("CFrame Update", Enum.RenderPriority.Camera.Value, UpdateCFrame)
Sorry if this post is messy or not well explained I am answering from my phone xd. I’m not even sure if this would work