Rotate and Position CFrame

I’ve never played around with CFrames and I have no clue how to approach this problem. I’ve tried to use ToObjectSpace but that didn’t return the correct results(probably the way I applied it). I’m ray casting and I want the model to face the same direction of the ray and also move to where the ray hit. I’m looking for the best way to do this.

Here is the script:

game:GetService("RunService").Heartbeat:Connect(function()
	--Ray calculation
	local ray = Ray(humanPosition.Position, CFrame.new(humanPosition.Position, game.Workspace.Part.Position).lookVector * 1)
	local hit, position = Workspace:FindPartOnRayWithWhitelist(ray, {}, true)
	
	local cframe = CFrame.new(humanPosition.Position, position)
	
	human:SetPrimaryPartCFrame(cframe)
	
	wait()
end)

The current cframe here just looks at the part; the human doesn’t move to it. I’m trying to move the human and make it look in the same direction as the ray.

There are many different ways to do this, but a simple way is to make the hit position “look at” the ray origin then simply rotate it by 180 degrees (flipping it to look away from the origin), something like this:

local cframe = CFrame.new(position, ray.Origin) * CFrame.Angles(0, math.rad(180),0)
human:SetPrimaryPartCFrame(cframe)
1 Like