Need help with pathfinding and motor6ds

I’m trying to make an NPC with a pathfinding system that works with arms aiming at a player, but I keep going in circles because pathfinding breaks the aiming, when it has to take a path that faces away from the player, by the way the NPC I’m making is holding a gun and that’s another problem because I can’t seem to make the arms and the handle (gun) rigid so it rotates positioned the same way as adjusted.

The arms are supposed to be posed like this:


and i wanna make it so that it rotates positioned like that

For the aiming I’ve been doing:

	local targetPosition = targetCharacter.HumanoidRootPart.Position
	local torso = NPC.PrimaryPart or Torso
	local lookPosition = Vector3.new(targetPosition.X, torso.Position.Y, targetPosition.Z)

	local handleCFrame = Handle.CFrame
	local directionWorld = (targetPosition - handleCFrame.Position).Unit
	local directionLocal = Torso.CFrame:Inverse():VectorToObjectSpace(directionWorld)
	local pitch = mathClamp(-math.asin(directionLocal.Y), 0, 0)

		if Neck then Neck.C0 = originalNeckC0 * CFrame.Angles(pitch, 0, 0) end
		if RightShoulder then RightShoulder.C0 = rightArmBaseC0 * CFrame.Angles(45, 0, -pitch) end
		if LeftShoulder then LeftShoulder.C0 = CFrame.new(-0.5, 0.5, -1) * CFrame.Angles(0, math.rad(-45), math.rad(-90)) * CFrame.Angles(45, 0, pitch) end
		if HandleJoint then HandleJoint.C0 = handleBaseC0 * CFrame.Angles(0, 0, -pitch) end

For pathfinding towards player:

local function chaseTarget(target)
	local path = PathfindingService:CreatePath()
	path:ComputeAsync((NPC.PrimaryPart or Torso).Position, target.Position)

	if path.Status == Enum.PathStatus.Success then
		for _, waypoint in ipairs(path:GetWaypoints()) do
			if Humanoid.Health <= 0 then return end
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				Humanoid.Jump = true
			end
			Humanoid:MoveTo(waypoint.Position)
			Humanoid.MoveToFinished:Wait()

			if workspace:FindPartOnRayWithIgnoreList(Ray.new((NPC.PrimaryPart or Torso).Position, (target.Position - (NPC.PrimaryPart or Torso).Position).Unit * 1000), {NPC}) then
				while workspace:FindPartOnRayWithIgnoreList(Ray.new((NPC.PrimaryPart or Torso).Position, (target.Position - (NPC.PrimaryPart or Torso).Position).Unit * 1000), {NPC}) do
					Humanoid:MoveTo(target.Position)
					task.wait(0.2)
				end
				break
			end
		end
	end
end

I’m all out of ideas, if anyone has had experience with this, please give ideas or advice on what to do.

I’ve dealt with similar NPC aiming and pathfinding issues in my pirates game the core problem is that Humanoid automatically rotates the whole character toward its movement direction which conflicts with manually aiming the arms and head also are you sure you Disabled AutoRotation?

Manually rotate the torso or HumanoidRootPart toward the current movement direction (the next path waypoint) but only on the horizontal plane

--This is exampled btw
local direction = (nextWaypoint.Position - HumanoidRootPart.Position).Unit
local horizontalDirection = Vector3.new(direction.X, 0, direction.Z).Unit 

HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position, HumanoidRootPart.Position + horizontalDirection) -- turn from vector3 into cf (I recommend using cf)

Keep your aiming code separate and update the arms and head to look at the target player independent of the body rotation. Your current arm posing logic looks solid just make sure you run it every frame or when the target moves For the gun/handle or anything like that yk make sure its welded or motor6d connected correctly to the hand so it moves rigidly with the arm rotations

2 Likes

tysm, this helped out alot, also i forgot to set autorotate to false which was causing most of the problems.

2 Likes

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