Making the LocalPlayer's camera point toward a Humanoid when the Humanoid is clicked

Hello all!

So I was wondering if there was any way to make the LocalCamera point directly towards a Humanoid that has been clicked right before?

If you are confused, basically I want the LocalCamera point directly at a different Humanoid(player) in-game when that said Humanoid(player) is click by the mouse.

Thanks you guys!

Camera has a property called CameraSubject. You can raycast (or utilize mouse.Hit mouse.Target) after a player clicks and determine whether that object belongs to another player through a simple conditional. Simply set CameraSubject to the head of the opposing player. If you’d like the camera to stay at its location while rotating according the other player’s direction, you can set a property of camera called CameraType to Enum.CameraType.Watch.

Sorry for asking this, but are able to provide some examples?

If you cannot think of any, that is fine!

Sure thing! Here’s a little script that I believe completes part of what you want accomplished:

local uis, plrs = game:GetService'UserInputService', game:GetService'Players';
local plr, char = plrs.LocalPlayer, script.Parent; --StarterCharacterScripts allows us to fetch Character without referencing Player!
local mouse, cam = plr:GetMouse(), workspace.CurrentCamera;

uis.InputBegan:Connect(function(input, processed)
	if processed or input.UserInputType ~= Enum.UserInputType.MouseButton1 then return; end --ensure the input was a left click and wasnt intended for clicking a GUI
	
	local hit = mouse.Target; --you can also raycast from the camera to mouse.Hit.Position for more customizability
	if not hit then return; end --the player probably had their mouse on the skybox when they clicked
	
	local head = hit.Parent:FindFirstChild'Head';
	if not head then return; end --the parent of the target doesnt own a child named 'head' so that probably means the target wasnt a player; you should probably implement further verifcation to ensure a player is clicked
	
	cam.CameraType = Enum.CameraType.Watch; --can set to anything but if you want the camera to be dead stationary you could set this to scriptable and change the cframe every renderstep
	cam.CameraSubject = head;
end)
1 Like

Thank you! This does work, but is there anyway to do exactly that, but keep the camera on the LocalPlayer?

Sorry about asking for a lot. This is crucial to my game soo… yeah lol.

Feel free to get back to me at any time suitable for you. There is no rush.

Before the event you add these 2 variables:

local CameraType = cam.CameraType
local CameraSubject = cam.CameraSubject

So all you gotta do to get the camera settings back is:

cam.CameraType = CameraType
cam.CameraSubject = CameraSubject

Thanks you guys! You really helped me!

If you’d like the camera to be stationary, you could change CameraType to Enum.CameraType.Scriptable and alter the CFrame every frame (RenderStepped) to keep the camera fixated towards the opposing player (as I suggested), but there could be another method of forcing the camera to stay where it is whilst having CameraType on Watch.

Here’s how I would go about the former option:

cam.CameraType = Enum.CameraType.Scriptable;
runSvc:BindToRenderStep('camUpdate', Enum.RenderPriority.Camera.Value + 1, function()
	cam.CFrame = CFrame.new(char.Head.Position, head.Position); --create a new cframe located at the head of the current player oriented towards the other player's head
end) --unbind 'camUpdate' and revert to camera's original settings as AstralBlu_e suggested you preserve when no longer needed
1 Like

One last thing, where do I put this code block in the script?

thx you guys!

The code snippet should supersede anything under the conditional for verifying the head of the other player. Do note that since we are utilizing RunService:BindToRenderStep(), the variable runSvc should be replaced with a reference to that service. I’ve also made some edits to the snippet due to some minor details I overlooked.

1 Like

And how would I do this exactly?

To get a service, we call :GetService() on the ServiceProvider (predefined variable game). :GetService() accepts a single string value representing the name of the service you want referenced as an argument; in this case, "RunService".

1 Like

Thank you! I am unable to test at the time, but when I do, I’ll make sure to let you know!