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

How do I make a dynamic 2d Camera the Zooms with the player?

Hello! I am making a 2d fighting game. I can make the 2d camera script, but I am not quite sure on how to make the camera similar to the Super Smash Bros camera. Is there any way to do this on ROBLOX? Thanks!

1 answer

✓ Accepted answer
local runService = game:GetService("RunService") 
local camera = workspace.CurrentCamera
local character = script.Parent.Parent.Character or script.Parent.Parent.CharacterAdded:Wait()

runService.RenderStepped:Connect(function() --update the camera and values on every frame
	local pos1 = character:WaitForChild("HumanoidRootPart").Position --get player 1's position
	local pos2 = workspace.Slime.PrimaryPart.Position --get player 2's position
	local between = (pos1 + pos2)/2 --get the middle position between the 2 positions
	local magnitude = (pos2-pos1).Magnitude -- get the distance of the 2 positions
	camera.CFrame = CFrame.new(Vector3.new(between.X,between.Y,between.Z-magnitude), between) -- update the camera's frame, the first vector3 on the left of the comma is the position of the camera. The further the 2 positions are, the farther back on the Z axis the camera will go. You can adjust the X, Y, and Z to your liking. The vector3 on the right of the comma is where the camera is looking at, which is the middle of 2 positions.
end)

If you have any questions let me know.

2 comments

0

Thank you a lot dude!

0

No problem, accept my answer if it helped so we can get those sweet sweet rep points :)

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