AlignOrientation not pointing to mouse correctly

Hi. I’m trying to make it where the player faces their mouse upon firing a gun. This is what happens.


My character will continue facing this direction, even after unequipping the weapon.

I tried making the AI (alignorientation) be set to no attachment after 5 seconds, this does not work.
Here’s the code for the AI.

	-----------------------------------------------------
	--Align character to mouse temporarily.
	local Focused = true
	task.spawn(function()
		while Focused == true do
			print("Projected.")
			wait()
			local pos = Mouse.Target.Position
			
			local unit = (pos - Character:FindFirstChild("HumanoidRootPart").Position).Unit
			local direction = Vector3.new(unit.X, Character:FindFirstChild("HumanoidRootPart").CFrame.LookVector.Y, unit.Z).Unit
			Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Facer").PrimaryAxis = direction
			Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Facer").SecondaryAxis = direction
			
			Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Facer").Attachment0 = Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("AngularAttachment")
		end
	end)
	delay(5, function()
		Focused = false
		Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Facer").Attachment0 = nil
	end)
	-----------------------------------------------------

And heres the script for setting up the AI, and the attachment it runs on.

--------------------------------------------------------
		--AlignOrientation for tools and stuff
		local Align = Instance.new("AlignOrientation")
		Align.Mode = Enum.OrientationAlignmentMode.OneAttachment
		Align.Parent = char:FindFirstChild("HumanoidRootPart")
		Align.Name = "Facer"

		local Attachment = Instance.new("Attachment")
		Attachment.Name = "AngularAttachment"
		Attachment.Parent = char:FindFirstChild("HumanoidRootPart")
		Align.MaxTorque = 10000
		Align.Responsiveness = 25
		--------------------------------------------------------

Any help is appreciated.

I have a 2d version of my game where my character points to the position of where the mouse is located, hopefully this helps you.

local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()

local char = player.Character or player.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

Humanoid.AutoRotate = false

camera.CameraType = Enum.CameraType.Scriptable

Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)

local cameraPart = Instance.new("Part")

cameraPart.Transparency = 1

cameraPart.CanCollide = false

cameraPart.Parent = workspace

cameraPart.CFrame = CFrame.new(HumanoidRootPart.Position + Vector3.new(0, 22, 0), HumanoidRootPart.Position)

local function onAim()
	if player.Character then
		local rootPart = player.Character:FindFirstChild("HumanoidRootPart")
		local mouseLocation = Vector3.new(mouse.Hit.X, rootPart.Position.Y, mouse.Hit.Z)
		rootPart.CFrame = CFrame.new(rootPart.Position, mouseLocation)
	end
end

ContextActionService:BindAction("Aim", onAim, false, Enum.UserInputType.MouseMovement)

RunService.RenderStepped:Connect(function()
    cameraPart.Position = HumanoidRootPart.Position + Vector3.new(0, 20, 0)
    camera.CFrame = cameraPart.CFrame
end)
2 Likes