Problem with cframe.lookat

im trying to make a character look at my character. every time i jump when im within distance of the other character, the character comes straight to me and shoots like 50 feet into the air lol. what could be the problem? i tried a few solutions via devforum but i havent gotten it to work yet. i appreciate any help ty!!

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
		local DISTANCE = 15
		local plrHum:Humanoid = c:FindFirstChildOfClass("Humanoid")
		local hrp = c:WaitForChild("HumanoidRootPart")
		local function update()
			local mag = (hrp.Position - catHRP.Position).Magnitude
			if mag < DISTANCE then
				cat.Head.CFrame = CFrame.lookAt(cat.Head.Position, c.Head.Position)
				if not playing then
					runAnimTrack:Play()
					playing = true
				end
			else
				runAnimTrack:Stop()
				playing = false
			end
		end
		connection = rs.Stepped:Connect(update)
		plrHum.Died:Connect(function()
			connection:Disconnect()
		end)
	end)
end)

Can you show me the script error?

1 Like

Maybe try anchoring the HumanoidRootPart of the dummy (if you are not willing to move it), otherwise you could change the Update() function a bit.

1 Like

hey nooby, im not getting any errors tbh. its just the cframe.lookat is what’s giving the trouble.

				catHRP.CFrame = CFrame.lookAt(catHRP.Position, hrp.Position)

hey king!!!
i would if i didnt need to move it but im planning to make the dummy walk backwards away from me if i walk close to it. the problem is with the lookat. its really weird tho. ty 4 the help as always btw !

				catHRP.CFrame = CFrame.lookAt(catHRP.Position, hrp.Position)

Have you tried rotating the matrix by 180 degrees? I am unsure how to do it but. Try negating the x and z axis

1 Like

hey nooby, thanks for your help mate.

i did a different approach to cframe.lookat to get it to work but i found a solution. cheers for the help tho!!

full solution:

local cat = workspace:FindFirstChild("catr6")
local jerry = workspace:FindFirstChild("jerryrigorsum")
local catHRP = cat.HumanoidRootPart
local cat_humanoid:Humanoid = cat:FindFirstChildOfClass("Humanoid")
local cat_animator:Animator = cat_humanoid.Animator
local rs = game:GetService("RunService")
local runanim = cat.run
local alignOrientation = cat.AlignOrientation
local alignPos = cat.AlignPosition
local runAnimTrack = cat_animator:LoadAnimation(runanim)
local connection = nil
local playing = false

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
		local DISTANCE = 15
		local plrHum:Humanoid = c:FindFirstChildOfClass("Humanoid")
		local hrp = c:WaitForChild("HumanoidRootPart")
		local function update()
			local mag = (hrp.Position - catHRP.Position).Magnitude
			local direction = (hrp.Position - catHRP.Position).Unit
			local angleinRads = math.atan2(direction.Z, direction.X)
			if mag < DISTANCE then
				-- if i did +angleinRads instead of -angleinRads, if i moved left, it would rotate the npc right. - math.rad(90) faces
				--the npc towards me instead of having his side profile facing toward my character.
				catHRP.CFrame = CFrame.new(catHRP.Position) * CFrame.Angles(0, -angleinRads - math.rad(90), 0)
				if not playing then
					runAnimTrack:Play()
					playing = true
				end
			else
				runAnimTrack:Stop()
				playing = false
			end
		end
		connection = rs.Stepped:Connect(update)
		plrHum.Died:Connect(function()
			connection:Disconnect()
		end)
	end)
end)

changes i made to get the npc to face me and not bug out:

			local direction = (hrp.Position - catHRP.Position).Unit
			local angleinRads = math.atan2(direction.Z, direction.X)
			if mag < DISTANCE then
				-- if i did +angleinRads instead of -angleinRads, if i moved left, it would rotate the npc right. - math.rad(90) faces
				--the npc towards me instead of having his side profile facing toward my character.
				catHRP.CFrame = CFrame.new(catHRP.Position) * CFrame.Angles(0, -angleinRads - math.rad(90), 0)

another simple solution was just to lock the y axis.

				catHRP.CFrame = CFrame.lookAt(catHRP.Position, Vector3.new(hrp.Position.X, catHRP.Position.Y, hrp.Position.Z))

math.rad(90) is just math.pi/2 :sweat_smile:
also it’s odd for the charater to directly point at you I’d recommend tweening.

1 Like

will do that soon enough ty man!!