So I am making a part where you rotate it with the mouse. The code I have works, but not in the way I want it to. I want it to be able to rotate, but stay in front of the player. what happens
So you want to have the Beam start from the Hand of the player and rotate trough the position the mouse is clicking or stay, i’m not 100% sure but i think this could help
local function ConvertToVec2(Vector)
local Camera = game.Workspace.CurrentCamera
local screenSize = Camera.ViewportSize
Vector = (Vector2.new(Vector.X, Vector.Y) - screenSize/2) * (2/screenSize)
return Vector3.new(Vector.X, -Vector.Y, 0)
end
game.ReplicatedStorage.RemoteEvents.MousePosition.OnServerEvent:Connect(function()
local Camera = game.Workspace.CurrentCamera
local CenterPos = ConvertToVec2(Camera:WorldToScreenPoint(character.HumanoidRootPart.Position))
local Position = ConvertToVec2(UIS:GetMouseLocation())
local Difference = Position - CenterPos
local Angle = math.atan(Difference.X/Difference.Y)
if Position.Y > CenterPos.Y then
Angle = Angle + math.rad(180)
end
beam.CFrame = CFrame.new(character.HumanoidRootPart.Position) * CFrame.fromAxisAngle(Vector3.FromNormalId(Enum.NormalId.Front), Angle) * CFrame.new(0, -beam.Size.Y/2, 0)
end)