How to make a part look at a player?

trying to change a parts orientation to always look at player

in starter player in starterplayer scripts i added a located script with the following code. the other stuff is NPCS which stare at the player

script:

script.Parent.MouseButton1Click:Connect(function(plr)
	if script.Parent.Parent.TextBox.Text == "v" then
		script.Parent.Parent.Parent.Error.Text = "Password Accepted. Welcome"
		script.Parent.Parent.Parent.Error.TextColor3 = Color3.new(0, 1, 0.14902)
	else script.Parent.Parent.TextBox.Text = " "
		script.Parent.Parent.Parent.Error.TextColor3 = Color3.new(1, 0, 0.0156863)
		wait(0.5)
		script.Parent.Parent.Parent.Error.Text = "Incorrect Password. Access Denied"
		wait(0.5)
		script.Parent.Parent.Parent.Error.Text = " "
		wait(0.5)
		script.Parent.Parent.Parent.Error.Text = "Incorrect Password. Access Denied"
		wait(0.5)

		local ui = script.Parent.Parent.Parent.Parent.Die
		local menu = script.Parent.Parent.Parent.Parent.Menu


		print("hit")

		script.Parent.Gunshot:Play()
		
		local player = game:GetService("Players")
		local humanoid = player:WaitForChild("Humanoid")

		humanoid.Health = 0

		ui.Visible = true
		local Death2 = plr:WaitForChild('PlayerGui'):WaitForChild("ScreenGui"):WaitForChild("Death2")
		wait(0.01)
		Death2.Visible = true
		ui.Visible = false

		wait(1)
		plr:Kick("You Died")

		print("everything good")
		print("HOPE")
	end
end)

SIDE NOTE:

The part is CAM1 and CAM2 they are PARTS not models

the rest are humanoids and are working!

2 Likes

I didn’t find any camera code in the script you provided… But all you need to do is use a CFrame.lookat() constructor to make the object look at the player.

example:

local charToLookAt = <some char>
myCamera.CFrame = CFrame.lookAt(myCamera.Position, charToLookAt.HumanoidRootPart.Position)
5 Likes

the camera does nothing but look at the player

1 Like

im kind of getting confused with your script example. so far i put

local plr = game.Players.LocalPlayer

local char = plr.Character or plr.CharacterAdded:Wait()

local humanoid = char:WaitForChild("Humanoid")

local root = char:WaitForChild("HumanoidRootPart")

local dummy = game.Workspace.Robber

local dummy1 = game.Workspace.Robber1

local dummy3 = game.Workspace.Robber3

local dummy2 = game.Workspace.Robber2

local cam1 = game.Workspace.Camera1

local cam2 = game.Workspace.Camera2

while true do

wait(0.1)

wait(0.001)

dummy.HumanoidRootPart.CFrame = CFrame.lookAt(dummy.HumanoidRootPart.Position, root.Position)

dummy1.HumanoidRootPart.CFrame = CFrame.lookAt(dummy1.HumanoidRootPart.Position, root.Position)

dummy2.HumanoidRootPart.CFrame = CFrame.lookAt(dummy2.HumanoidRootPart.Position, root.Position)

dummy3.HumanoidRootPart.CFrame = CFrame.lookAt(dummy3.HumanoidRootPart.Position, root.Position)

cam1.CFrame = CFrame.lookat(cam1.Position, charToLookAt.HumanoidRootPart.Position)

cam2.Orientation.CFrame = CFrame.lookAt(cam2.Position, root.Position)

end

but im kind of getting confused at “CharToLookAt”

2 Likes

you need to change camera fixed to scriptable, i hope this work !

1 Like

this is a part not minipulating the players camera

i just updated the post. reread it ok

I have to say that this would be really cool to see in games,

Asides from that. CFrame is your best bet here.

Simply set the part’s CFrame to something like this:

camPart.CFrame = CFrame.new(camPart.Position, ***players character***.HumanoidRootPart.Position)

First argument is the Cam’s Position, while the 2nd argument with a Comma is the position for the target to LOOK at.

I hope this helps.

i tried


local plr = game.Players.LocalPlayer

local char = plr.Character or plr.CharacterAdded:Wait()

local humanoid = char:WaitForChild("Humanoid")

local root = char:WaitForChild("HumanoidRootPart")

local dummy = game.Workspace.Robber

local dummy1 = game.Workspace.Robber1

local dummy3 = game.Workspace.Robber3

local dummy2 = game.Workspace.Robber2

local cam1 = game.Workspace.Camera1

local cam2 = game.Workspace.Camera2

while true do

wait(0.1)

wait(0.001)

dummy.HumanoidRootPart.CFrame = CFrame.lookAt(dummy.HumanoidRootPart.Position, root.Position)

dummy1.HumanoidRootPart.CFrame = CFrame.lookAt(dummy1.HumanoidRootPart.Position, root.Position)

dummy2.HumanoidRootPart.CFrame = CFrame.lookAt(dummy2.HumanoidRootPart.Position, root.Position)

dummy3.HumanoidRootPart.CFrame = CFrame.lookAt(dummy3.HumanoidRootPart.Position, root.Position)

cam1.CFrame = CFrame.new(cam1.Position, char.HumanoidRootPart.Position)

cam2.CFrame = CFrame.new(cam2.Position, char.HumanoidRootPart.Position)

but the camera looks straight down

(BTW IGNORE DUMMY1, DUMMY2, DUMMY3 since they are humanoid ai)

Add a decal onto the Camera part. If the front of the camera doesn’t have that orange outline, that means the Camera part was incorrectly sized to having the front side somewhere else instead of where you wanted it.

Hence the reason it would look down, because the “Front” side of the camera part is probably the bottom side of the camera part.

Heres what I am talking about:

The Decal instances are all marked on front side.

2 Likes

This is essentially exactly what I already gave him to use. The version you provided is basically deprecated in favor of CFrame.lookAt()

Old version:

CFrame.new(position, position)

NewVersion:

CFrame.lookAt(position, position)

The new version also includes an optional UpVector parameter to further increase the accuracy of the rotation.

1 Like

Now he has a problem where the Camera is facing down, I am showing another post of mine to show how to fix this “looking down” issue. And I think it might be an incorrect sizing of the camera where the bottom is the “Front” side.