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.
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
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
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
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