How would I make a body-follow-mouse script?

local AngleRange = math.rad(90)

runService.RenderStepped:Connect(function()
    local Direction = ((PlayerMouse.Hit.p - character.HumanoidRootPart.Position) * Vector3.new(1, 0, 1))
    local Goal = CFrame.new(character.HumanoidRootPart.Position, character.HumanoidRootPart.Position + Direction)

    local Angle = math.acos(math.clamp(Camera.CFrame.LookVector:Dot(Goal.LookVector), -1, 1))

    if Angle <=AngleRange then
        character.HumanoidRootPart.CFrame = Goal
    end
end)

^ Credits to Luacrative

How would I tween my body-follow-mouse script so that it doesnt instantly snap to my mouse position and instead smoothly rotates towards it?

Use TweenService

local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local Character = game.Players.LocalPlayer.Character
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Camera = game.Workspace:WaitForChild("Camera")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local AngleRange = math.rad(90)

RunService.RenderStepped:Connect(function()
    local Direction = ((Mouse.Hit.p - HumanoidRootPart.Position) * Vector3.new(1, 0, 1))
    local Goal = CFrame.new(HumanoidRootPart.Position, HumanoidRootPart.Position + Direction)

    local Angle = math.acos(math.clamp(Camera.CFrame.LookVector:Dot(Goal.LookVector), -1, 1))

    if Angle <= AngleRange then
        local TweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Linear)
        local Tween = TweenService:Create(HumanoidRootPart, TweenInfo, {CFrame = Goal})
        Tween:Play()
    end
end)

1 Like

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