How would I CFrame a character to look towards a point

I am trying to make a character movement system by having an anchored character rather then an unanchored character because the physics are really expensive, and by doing this way I can increase performance drastically. So there has been a couple of topics of this subject, but for me nothing seems to work. I have tried doing
CFrame.new(charPos, targetPos)
And this seems to work, but then some weird issues occur where it looks in the opposite direction sometimes when walking and sometimes it doesn’t change direction at all. I am really confused, and I was wondering if anyone knows a way to make a character face in a direction towards a point

This is the result of turning the character using CFrame.new(charPos, targetPos)

Code that makes the character move towards that point
The code is messy, but its because I am trying a lot of different solutions so beer with me
If you have any clue on what could potentially be the issue please let me know. Anything helps at this point thank you. There could be an issue with the way I calculate the targetPos, charPos but I am not sure.

local function tweenEntityToPoint(char, targetPos, walkSpeed)
    --Get positions
	local charPos = char:GetPivot().Position
	local heightOffset = math.abs(charPos.Y - char.HumanoidRootPart.Position.Y)
	targetPos = Vector3.new(targetPos.X, targetPos.Y+heightOffset, targetPos.Z)
	charPos = char.HumanoidRootPart.Position
	--calc travel speed
	local disToEnd = (charPos - targetPos).Magnitude
	local travelSpeed = disToEnd/walkSpeed
    --finalize CFrame
	local x,y,z = CFrame.new(char.HumanoidRootPart.Position, Vector3.new(targetPos.X, char.HumanoidRootPart.Position.Y, targetPos.Z)):ToEulerAnglesXYZ()
	local newCF = CFrame.new(targetPos) * CFrame.Angles(0,y,0)
    --tween torwards point
	local tween = tweenService:Create(char.HumanoidRootPart, TweenInfo.new(travelSpeed, Enum.EasingStyle.Linear), {CFrame = newCF})
	tween:Play()
	tween.Completed:Wait()
end
2 Likes

Maybe u should try CFrame.lookAt().

After line local newCF = CFrame.new(targetPos) * CFrame.Angles(0,y,0) put CFrame.lookAt(charPos, targetPos)

Maybe it actually will work.

2 Likes

Hmm how would that work though

2 Likes

To be honest I would use path-finding for this, try using this.

1 Like

Assembling the greatest hood minds to solve this one

1 Like