How to make camera zoom out when two players are far away but zoom in when close?

I need help making a 2D fighting game but I cant figure out the camera mechanics,

When two players are close together the camera is close to them.

When two players are far away the camera would zoom out to keep them in the camera.

2 Likes

how about checking magnitudes of both of their positions and do calculations for the camera position?

1 Like

one way you could approach it is,

  1. get the magnitude distance between the 2 characters
  2. find the position that is directly in the middle of the 2 using the distance and subtracting that from both the characters positions
  3. find a good offset for your camera and fov and add that to the position calculated previously
  4. tween the camera to that position calculated and run that function every heartbeat
  5. (optional) add a barrier where your fov ends on the left and right side to stop the characters from going out of bounds

PS :
sorry i couldn’t give you a script i just didn’t have time to script a whole system, hopefully this helps
:smile:

3 Likes

Oh lol

Glad i could help
I’m excited to see this game when it’s released
can i have the name?

It’d be a 2D street fighter remake so it’d probably be like “Street Bloxer”

Heres the code I have so far by the way but i’m having trouble making a barrier for the camera to stop going out of bounds like you said

local player = game.Players.LocalPlayer
local character = player.Character
local camera = workspace.Camera

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

RunService.RenderStepped:connect(function()
	local Part = workspace.Part
	
	local magnitude = (Part.Position - character.HumanoidRootPart.Position).Magnitude
	local middlePos = (Part.Position+character.PrimaryPart.Position)/2
	
	local goal = {}
	goal.CFrame = CFrame.new(middlePos) + Vector3.new(0, 0, magnitude)
	
	if goal.CFrame.X > 25 then -- trying to add a barrier
		print('too far')
	end
	local tweenInfo = TweenInfo.new(1)
	local tween= TweenService:Create(camera,tweenInfo,goal)
	tween:Play()
end)

Figured it out!

local player = game.Players.LocalPlayer
local character = player.Character
local camera = workspace.Camera

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

RunService.RenderStepped:connect(function()
	local Part = workspace.Part
	
	local magnitude = (Part.Position - character.HumanoidRootPart.Position).Magnitude
	local middlePos = (Part.Position+character.PrimaryPart.Position)/2
	camera.CameraType = Enum.CameraType.Scriptable; --so script can change it
	
	local goal = {}
	goal.CFrame = CFrame.new(middlePos) + Vector3.new(0, 0, magnitude)
	
	if CFrame.new(middlePos).X > 40 then
		goal.CFrame = CFrame.new(0,10,30)
	elseif CFrame.new(middlePos).X < -40 then
		goal.CFrame = CFrame.new(0,10,30) 
	else
		goal.CFrame = CFrame.new(middlePos) + Vector3.new(0, 0, magnitude)
	end
	
	local tweenInfo = TweenInfo.new(1)
	local tweeen = TweenService:Create(camera,tweenInfo,goal)
	tweeen:Play()
end)
3 Likes

Wow good job,
again im really glad i could help :smile:

I’m guessing you’re trying to make one of those 2D games. I wish you good luck.

1 Like

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