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

Problem with a 2D Script?

The code doesn't work very good :( Can someone help me how to make that focussing of the cam smoother?

local cam = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local focus = Instance.new("Part", game.Workspace)
focus.Anchored = true
focus.CanCollide = false
focus.Transparency = 1
local bp = Instance.new("BodyPosition", focus)
bp.D = 1250
bp.P = 10000
bp.position = focus.Position
bp.maxForce = Vector3.new(0, 0, 1000000)

while wait() do
	focus.Position = plr.Character.Torso.Position
	bp.position = focus.Position
	cam.CameraSubject = focus
	cam.CameraType = Enum.CameraType.Attach
	cam.CoordinateFrame = CFrame.new(0,10, 10)
end

2 answers

✓ Accepted answer
  1. We can make bp's maxForce infinity
  2. We can use RunService RenderStepped

Infinity

We can make the maxForce infinity using math.huge.

local infinity = math.huge

RenderStepped

In order to get RenderStepped we need to get to the RunService.

game:GetService("RunService") --Done.

Now we need to get RenderStepped, which is an event.

game:GetService("RunService").RenderStepped --This only works in LOCAL SCRIPTS

Now we just add a connection and a function and an end.

game:GetService("RunService").RenderStepped:connect(function()
	print("Foo")
end)

RunService is 2x faster than wait().



here is your script if you followed everything I told you to do.

local cam = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local focus = Instance.new("Part", game.Workspace)
focus.Anchored = true
focus.CanCollide = false
focus.Transparency = 1
local bp = Instance.new("BodyPosition", focus)
bp.D = 1250
bp.P = 10000
bp.position = focus.Position
bp.maxForce = Vector3.new(math.huge,math.huge,math.huge)

game:GetService("RunService").RenderStepped:connect(function()
    focus.Position = plr.Character.Torso.Position
    bp.position = focus.Position
    cam.CameraSubject = focus
    cam.CameraType = Enum.CameraType.Attach
    cam.CoordinateFrame = CFrame.new(0,10, 10)
end)



Also, instead of making the camera's target the focus, we can use interpolate instead. Interpolate can make your camera slide smoothly. This way, we don't rely on parts and stuff.


Example of Interpolate:

--LocalScript
workspace.Camera:Interpolate(50,10,15, game:GetService("Players").LocalPlayer.Character.HumanoidRootPart, 5)

--50,10,15 is the Position where the camera will go, the HumanoidRootPart is the focus. 5 is how many seconds it will take to get to the position. The position could also be the character's Torso!





Hope this helps!

1 comment

0

Thanks, I already finished that | And thanks for your time on typing all that explaination :O that's the only reason I accepted your answer...

If you change the wait() in your while do function to game:GetService('RunService').RenderStepped:wait()then it should run a lot smoother!

4 comments

0

Even better: hook it up directly to RenderStepped instead of looping in the first place.

0

Ok, I don't know what RunService and Render and all is...

0

Need help

0

You literally just replace the word 'wait()' with game:GetService('RunService').RenderStepped:wait()

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