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

Custom 2-D fighting Super Smash Bros like Camera working except for one small part?

Asked by 6 years ago
-- Ultimate 2-D Fighitng Camera script
-- Version 1.0
-- Created by SpyGuyTBM

local X = {}
local Y = {}


local cam = workspace.CurrentCamera
cam.CameraType = "Scriptable"
wait(5)
while true do
wait(0.1)
for _,plr in pairs(game.Players:GetPlayers()) do

table.insert(X, plr.Character.Torso.CFrame.X )
table.insert(Y, plr.Character.Torso.CFrame.Y)
end


table.sort(X)
table.sort(Y)


local Xlow = X[1]
local Xhigh = X[#X]

local Xcam = (Xlow + Xhigh)/2


local Ylow = Y[1]
local Yhigh = Y[#X]

local Ycam = (Ylow + Yhigh)/2


local Xleng = Xhigh - Xlow

local NewXleng = Xleng/2

local Zcam = 1.42814801*NewXleng

cam:Interpolate(CFrame.new(Xcam,Ycam,Zcam + 10)  , CFrame.new(Xcam,Ycam,5), 0.1)



 X = {}
 Y = {}
    end



ok, before i state my question, i would like to thank the scripting helpers community for being helpful in my journey in scripting. One month ago i was a complete noob, and now i feel proud at how far i have come.

Anyways, enough with the sentimental stuff.

So this script is meant to replicate super smash bro's camera that zooms out when fighters move away, and zooms in when they come closer together.

And it works for the most part, try it out! It's really cool.

But, I cant get the camera to stay AT LEAST 10 studs away

cam:Interpolate(CFrame.new(Xcam,Ycam,Zcam + 10) , CFrame.new(Xcam,Ycam,5), 0.1)

in this line of code "Zcam + 10" is not pushing the camera 10 studs back. Why is this?

1 answer

Log in to vote
0
Answered by
saenae 318 Moderation Voter
6 years ago
Edited 6 years ago

Hey SpyGuyTBM!

So, at the moment, your code isn't at all taking into account your players' position on the Z, which is why it's not being pushed back if your character walks toward your camera. In order to fix this, you simply have to find the character who's torso is nearest to your camera, and then zoom back by however many studs you like from their position, like so:

local X = {}
local Y = {}
local Z = {}

local cam = workspace.CurrentCamera
cam.CameraType = "Scriptable"
wait(5)

while true do
    wait(0.1)
    for _,plr in pairs(game.Players:GetPlayers()) do
        table.insert(X, plr.Character.Torso.CFrame.X)
        table.insert(Y, plr.Character.Torso.CFrame.Y)
        table.insert(Z, cam.CoordinateFrame.Z-plr.Character.Torso.CFrame.Z)
    end

    table.sort(X)
    table.sort(Y)
    table.sort(Z)

    local lowest_z = Z[1]

    local Xlow = X[1]
    local Xhigh = X[#X]

    local Xcam = (Xlow + Xhigh)/2


    local Ylow = Y[1]
    local Yhigh = Y[#X]

    local Ycam = (Ylow + Yhigh)/2


    local Xleng = Xhigh - Xlow

    local NewXleng = Xleng/2

    local Zcam = 1.42814801*NewXleng

    local num_z = (cam.CoordinateFrame.Z-lowest_z)+20;
    cam:Interpolate(CFrame.new(Xcam,Ycam,Zcam + num_z), CFrame.new(Xcam,Ycam,5), 0.1)

    X = {}
    Y = {}
    Z = {}
end

[I made the minimum distance from the camera 20, to make the zoom a bit more clear]

In the end, though, this effect is a little bit wonky (since players can basically minify other players by walking forward), so I'd recommend making borders both in front and behind the players on the Z, so that they can't walk too far in those directions. That'll give you a bit more of a Smash-like vibe.

Hope that helps! =)

P.S. For future reference, tab your code properly, readability is extremely important so that both other programmers and you yourself have an easy time understanding it later on =P

1
thanks! User#17125 0 — 6y
Ad

Answer this question