I am making a FPS and I am trying to avoid using a view model. I am attempting to make the arms point towards the mouse position and I have came up with the following:
local frontVector: Vector3 = humanoidRootPart.CFrame.LookVector
local topVector: Vector3 = (humanoidRootPart.CFrame * CFrame.new(math.pi / 2, 0, 0)).LookVector
local mouseVector: Vector3 = mouse.Hit.LookVector
local xAngle: TypeUtil.double = math.acos(mouseVector:Dot(topVector) / (topVector.Magnitude * mouseVector.Magnitude))
local zAngle: TypeUtil.double = math.acos(mouseVector:Dot(frontVector) / (frontVector.Magnitude * mouseVector.Magnitude
I create two look vectors that emit from the top and front of the player. I then calculate their angles (not sure if correctly) based off the mouse vector. I am sure I am doing this next part wrong, so can anyone suggest how to fix this? The full code is bellow:
movementThread = RunService.RenderStepped:Connect(function(): nil
local cameraPoint: Vector3 = mouse.Hit.Position
local headPosition: Vector3 = head.CFrame.Position
local frontVector: Vector3 = humanoidRootPart.CFrame.LookVector
local topVector: Vector3 = (humanoidRootPart.CFrame * CFrame.new(math.pi / 2, 0, 0)).LookVector
local mouseVector: Vector3 = mouse.Hit.LookVector
local xAngle: TypeUtil.double = math.acos(mouseVector:Dot(topVector) / (topVector.Magnitude * mouseVector.Magnitude))
local zAngle: TypeUtil.double = math.acos(mouseVector:Dot(frontVector) / (frontVector.Magnitude * mouseVector.Magnitude))
if (isMovementEnabled) then
print(math.deg(math.pi / 2 - xAngle))
TweenService:Create(rightShoulder, moveTweenInfo, {
C0 = rightShoulderCFrame * CFrame.Angles(xAngle, zAngle, 0)
}):Play()
end
return nil
end)