Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Adjusting my 2D camera so it keeps 2 players in view?

Asked by
crome60 43
7 years ago
Edited 7 years ago

So, I'm trying to make a 2D camera for my fighting game-esque script, but the camera only moves along the X axis, I'm trying to make sure that it aligns with the direction that the player is facing. So I want to use lookVector, but I'm unsure on how I would incorporate it into my script. I decided to look on the wiki and found an article (http://wiki.roblox.com/index.php?title=Making_a_2D_Platformer) that gave me a basis on where to start on it, but I'm not sure where to continue. So I've been experimenting with lookVector, getting the midpoint of the player and target's torso's and trying to rotate around that. So now the problem is figuring out which method I should use and how I should actually use it. Also, how would I even check to see if a humanoid is moving side to side so I can adjust the camera position properly?

This is a good representation of the issue (http://imgur.com/a/lHxuI)

Here is my code located in the CameraScript:

repeat wait() until _G.arbDistance and _G.torsoDir -- Magnitude of player to target distance and lookVector of the player

local zoom = -.15-- Decreases as the player gets closer to their target [ZOOM]
local zoomRange = {-.2, .2} -- Max zoom ranges for the left and right sides
local rot = -10 -- Gets closer to zero as the player is further away from their target [ROTATION]
local rotRange = {-10, 10} -- Max rotation ranges for the left and right sides
local angles = CFrame.Angles
local cameraSpeed = .25
local baseCamHeight = 5 
local camHeight = 0 -- Increases as the player gets farther away from the ground
local cameraZOffset = 25 -- Initial zoom
local cameraXChase = 20 -- Decreases as the player gets closer to their target [PLAYER-CAM POSITON] 
                        -- Also represents how far the player can be for the camera to start moving

_G.side = "left" -- player always starts at the left side of the screen when they lock on to an enemy

local distFromGround 
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')

local angle = 0 -- the starting angle of the camera, before rotating 

local function setupCamera()
    camera.CFrame = CFrame.new(Vector3.new(0,baseCamHeight,cameraZOffset), Vector3.new(0,baseCamHeight,0))
end
setupCamera()
player.CharacterAdded:connect(setupCamera)

local function onUpdate()
    if player.Character and player.Character:FindFirstChild('Torso') then

        local playerX = player.Character.Torso.Position.X
        local cameraX = camera.CFrame.p.X 
        local mp = CFrame.new(_G.tp1:Lerp(_G.tp2, .5)) -- midpoint of the player and target

        if cameraX - cameraXChase < playerX and _G.advancing == true then -- advancing!
            camera.CFrame = camera.CFrame + Vector3.new(cameraSpeed, camHeight, zoom) -- moves the camera forward
        elseif cameraX - cameraXChase > playerX and _G.advancing ~= true then -- retreating!
            camera.CFrame = camera.CFrame - Vector3.new(cameraSpeed, camHeight, zoom) -- moves the camera backward
        end

        if cameraXChase < 0 then -- Checks what side the player is on
            _G.side = "right"
        elseif cameraXChase > 0 then
            _G.side = "left"
        end     
    end
end

RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value, onUpdate)

If anyone could help, I would be incredibly grateful.

0
I generally get what you're saying, but maybe a little elaboration on how the camera behaves would help. For instance, does it zoom out as the distance between target and player increases? How is rotation supposed to be handled? Should the camera remain orthographic, or does it dynamically rotate around the scene? nicemike40 486 — 7y
0
Essentially, the camera is supposed to zoom out as the player gets further from their target, rotation is supposed to be handled to be able to keep 2 humanoids in view of eachother, like shown in this video: https://www.youtube.com/watch?v=jixEo1571FE crome60 43 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

Get the angle of the two players (or the farthest two players) by making a hyptohetical right triangle and finding the angle. This will help you with the rotation of the camera.

Ad

Answer this question