How to make a player look towards another player

Hello! I’m currently trying to fix an issue on how to get a player to look towards another player!
Here’s the current script:

function:

local function lookAt(Character, Target) --assume chr is a character and target is a brick to look towards
	if Character.PrimaryPart then --just make sure the character's HRP has loaded
		local chrPos = Character.PrimaryPart.Position --get the position of the HRP
		local tPos = Target.Character.PrimaryPart.Position --get the position of the target
		local modTPos = Vector3.new(tPos.X,chrPos.Y,tPos.X) --make a position at the target, but with the height of the character
		local newCF = CFrame.new(chrPos,modTPos) --create our CFrame
		Character:SetPrimaryPartCFrame(newCF) --set the HRP's CFrame to our result, thus moving the character!
	end
end

based from here

RunService Loop:

Run_Service.RenderStepped:Connect(function()
		if Character and Humanoid_Root_Part and Equip == true then
			
			local NewCFrame
			
			Closest_Player = GetClosest()
			
			if not Closest_Player then
				Character.Humanoid:UnequipTools()
				Enemy_Remote:FireServer("Combat ended")
			end
			
			if Closest_Player then
				lookAt(Character, Closest_Player)
			end
			
			--print(Closest_Player)
			if Fired == false then
				print(Closest_Player)
				Enemy_Remote:FireServer(Closest_Player)
				print("Fired!")
				Fired = true
			end
			
			local Start_CFrame = CFrame.new((Humanoid_Root_Part.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(X_Angle), 0)*CFrame.Angles(math.rad(Y_Angle), 0, 0)

			local Camera_CFrame = Start_CFrame + Start_CFrame:vectorToWorldSpace(Vector3.new(Camera_Position.X,Camera_Position.Y,Camera_Position.Z))
			local Camera_Focus = Start_CFrame + Start_CFrame:vectorToWorldSpace(Vector3.new(Camera_Position.X,Camera_Position.Y,-50000))

			Player_Camera.CFrame = CFrame.new(Camera_CFrame.p,Camera_Focus.p)
			
			--local lookVec = Character:WaitForChild("HumanoidRootPart").CFrame.LookVector
			--local Test = CFrame.lookAt(Player_Camera.CFrame.Position, Player_Camera.CFrame.Position + lookVec)
			
			--Player_Camera.CFrame = CFrame.new(Camera_CFrame.p,Camera_Focus.p)
		end
	end)
end)

It’s leading to some weird and wacky results showin in the video below!

It’s almost like it’s still using the previous position but i have no idea why, as i’m sending it the new position every frame. Can anyone help me out? All help is appreciated! I’ve also heard of using bodymovers and if someone could help me with that, i’d also be fine with moving to that method!

3 Likes

Position doesn’t work well for character parts so use CFrame.Position, So I would do it like this:

local function lookAt(Character, Target) --assume chr is a character and target is a brick to look towards
	if Character.PrimaryPart then --just make sure the character's HRP has loaded
		local chrPos = Character.PrimaryPart.CFrame.Position --get the position of the HRP
		local tPos = Target.Character.PrimaryPart.CFrame.Position --get the position of the target
		local modTPos = Vector3.new(tPos.X,chrPos.Y,tPos.X) --make a position at the target, but with the height of the character
		local newCF = CFrame.new(chrPos,modTPos) --create our CFrame
		Character:SetPrimaryPartCFrame(newCF) --set the HRP's CFrame to our result, thus moving the character!
	end
end
1 Like

Hello! Thank you for the response! I’ve tried it out and unfortunately it still has the same issue

When you want to only rotate on the y axis, you can do this:

local modTPos = (chrPos - tPos).Unit * Vector3.new(1,0,1) --mask out the Y axis
local upVector = Character.PrimaryPart.CFrame.UpVector
local newCF = CFrame.lookAt(chrPos, chrPos + modTPos, upVector) * CFrame.fromAxisAngle(Vector3.new(0, 1, 0), math.pi)
2 Likes

Thanks for the help but:


I think i’m going to switch to bodymovers as they seem more reliable

Sorry, see the edited code I posted. I forgot how CFrame.lookAt behaved.

1 Like

Thanks for the response once again! However, it’s still acting a bit weird

Sorry, edited again. I was on mobile and wrote the matrix order wrong.

Thank you for the continued assistance! However, it’s still acting very buggy

My brain isn’t working today. If I was on pc I’d test it before, but yeah should be the final edit. I always forget that matrix is Right, Up, Forward and not Forward, Up, Right

It’s all good! Not many people would persevere to help others especially when on mobile! With this, there’s partial success when keeping my previous body gyros in it to prevent it from flying away as shown in the video:

It also manages to inverse the faces (i’ve got no idea how lol) and it makes the raycasted hitboxes act on the other side which leads me to think that it’s reversing everything :thinking:

Edit: I forgot to mention that the first testing is without body gyros, and in the second testing i uncomment them

I dont think you should use a bodygyro. Just keep using the code you did before, while the character’s left side was facing the target and it should work.

yeah the bodygyro was disabled and i just wanted to test what would happen if it was enabled and it did alleviate some of the issues, albeit just a bandaid

here’s a video of just the script, no body gyros

I’ve switched it to use lookAt which should hopefully work

It’s gone back to facing the other way again

Also once again, i appreciate your help!

Alright I just added something to rotate it 180 degrees on the Y axis, should hopefully work now.

It seems to be working perfectly! Thank you for your help for all this time! If i experience issues on this in the future by further testing would i be able to ask you for help? It’s fine if not as you’ve already provided tons of help and support!

Sure, I don’t see why not.ttttttttttttt

1 Like