Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I slow an animation down based on the distance of the mouse from the center of the screen?

This rotates the players Camera in a specific direction depending on where the mouse is at on the screen. It works fine but now I want to change it so that the closer the mouse is to the center of the screen the slower the camera will turn.

local RunService = game:GetService("RunService")

local Rotate = 0



RunService.RenderStepped:Connect(function()
	Camera.CFrame = Character.Head.CFrame
	
	if Mouse.X > Mouse.ViewSizeX/2 + 50 then
		Rotate = -1 -- This is the increment at which the camera will turn. This number needs to become smaller the closer the mouse is to the center of the screen.
	elseif Mouse.X < Mouse.ViewSizeX/2 - 50 then
		Rotate = 1
	else
		Rotate = 0
	end
	
	Character.HumanoidRootPart.CFrame = Character.HumanoidRootPart.CFrame * CFrame.Angles(0, math.rad(Rotate), 0) * CFrame.new()
	Char:SetLocalTransparency()
end)

1 comment

0

Actually, to be a little more precise, the camera needs to come to a complete stop by the time the mouse is within the "DeadZone" which is the center of the screeen with a margin of 50.

1 answer

✓ Accepted answer

Try a cosine function?

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer.PlayerGui
local Screen = PlayerGui:FindFirstChildOfClass("ScreenGui") or Instance.new("ScreenGui", PlayerGui)
local Camera = workspace.CurrentCamera
local Mouse = LocalPlayer:GetMouse()
local pi = math.pi


local function CharacterAdd(Character)
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	OpenConnection = RunService.RenderStepped:Connect(function()
		Camera.CFrame = Character.Head.CFrame
		HumanoidRootPart.CFrame = HumanoidRootPart.CFrame * CFrame.Angles(0, math.rad(math.cos(pi * Mouse.X / Screen.AbsoluteSize.X)), 0)
		--Character:SetLocalTransparency()
	end)
end

local Character = LocalPlayer.Character
LocalPlayer.CharacterAdded:Connect(CharacterAdd)

if Character then
	CharacterAdd(Character)
end

EDIT: Upon request, I will explain why it works as best as I can. A cosine function is a wave function that oscillates between -1 and 1.

Here are some (x, y) important values for the cosine function: (0 , 1) (0.5*pi, 0) (pi , -1)

Now if you look at what you want to achieve in terms of X position of mouse and the Rotate value in your first script

(0 , 1) (0.5 * Screen.AbsoluteSize.X, 0) (Screen.AbsoluteSize.X , -1)

If you multiply the second set of ordered pairs by pi and divide by Screen.AbsoluteSize.X, you would get the first set, and the inverse of those operations can be applied to the first set to get the second.

I guess that isn't the best way to explain it, but basically, the cosine wave from 0 to pi is stretched to the length of the entire screen. You could have just used a linear function as well, but the cosine curve is rounded before its max and minimum, so it looks smoother because it stops speeding up so much and gets close to maximum speed the closer you move the mouse to the edge of the screen.

1 comment

0

Thanks for the answer. This works well. Unfortunately though, I don't understand why it works. If you have time, would you mind explaining this?

This discussion is preserved as it was. New answers, comments, and votes are closed.