How to make R6 npcs arms and head point towards player

Hello!
I have been working on a game, and one of the npcs wields a gun that… fires (woohoo)
However, since i made him, he sucks at aiming.

I want to make him not as terrible as aiming by making his head and arms look at player one ONE axis, kind of like what the scavs do in decaying winter (top down axis, i forgot what it was called)

However, since i am a little baby, I dont know how to do that. I spent 30 minutes searching on devforum to find nothing of use.

Any help is appreciated

1 Like

I think i found something you might find useful:

local part = game.Workspace.Part -- The part that is affected.
local partToFaceTo = game.Workspace.Part2 -- The part that the other part is going to face to.

part.CFrame = CFrame.new(part.Position, partToFaceTo.Position)

Alright so i figured out that if theres two values when you set the CFrame it will set the front surface of the first value to face the second value. Heres a example with a turret that follows you in a roblox studio:
TurretTest.rbxl (61.3 KB)
You can look in all of the scripts and stuff and get an idea of how you can attempt to do this

1 Like

Well yes, i know about Cframe:lookAt, however, for some reason, because i have pathfinding in my ai, it teleports the ai back when i change cframe of any object associated with the humananoidrootpart or torso

Just realized you didnt use cframe lookat… im kinda denseee

I will try to find another solution

i think ive found something that might solve this im going to test it

I will test whatever you come up with tomorrow as i am going to bed. Thanks!

Im getting somewhere:
ArmTest.rbxl (57.3 KB)
the arm is facing on the wrong surface. But i figured out that you can just edit C0 and C1 in the arm joint and i dont think it should not mess up the npc’s ai

1 Like

I Think i got it!
ArmTest.rbxl (57.0 KB)

1 Like

Ok. I will test this out.
(aaaa)

Oh, thats pretty cool! But i have one question, is it possible for the arms to only go up and down? I will still mark your answer as solution.

i think it is possible just edit the script around

1 Like

Let me know if you have anymore questions!

Oh yeah, I do also have one more question, Is it possible to use this script for r6?

Yes ill send you an updated script for an R6 dummy

1 Like
local arm = script.Parent.Torso["Right Shoulder"]
local NPC = script.Parent

function getClosestPlayer()
	local closest_player, closest_distance = nil, math.huge
	for i, player in pairs(workspace:GetChildren()) do
		if player:FindFirstChild("Humanoid") and player ~= NPC then
			local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
			if distance < closest_distance then
				closest_player = player
				closest_distance = distance
			end
		end
	end
	return closest_player
end

local cframe0 = arm.C0
while true do
	local player = getClosestPlayer()
	if player then
		local is_in_front = NPC.PrimaryPart.CFrame:ToObjectSpace(player:FindFirstChild("HumanoidRootPart").CFrame).Z < 0
		if is_in_front then
			local unit = -(NPC.PrimaryPart.CFrame.p - player.PrimaryPart.Position).unit 
			arm.C0 = cframe0 * CFrame.new(Vector3.new(0, 0, 0), unit) * CFrame.Angles(90, -math.rad(NPC.PrimaryPart.Orientation.Y), 0)
		end
	end
	wait()
end
3 Likes

I might mark this as solution instead. Thank you soo much!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.