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

How can I keep my camera at the right angle?

Asked by
Lamar 45
8 years ago

So at the moment, I've got a camera script that keeps both players on screen. However, I'd like to lock the camera to a side view (like a 2.5D fighting game, think Soul Calibur). Right now I'm using a lerp to find the center of the vector between the two players and positioning the camera there, and using the magnitude of the vector to keep both players on screen. I believe the lerp is screwing up the angle of the camera, when I want to keep it fixed.

The way I understand how I need to solve this problem is this:

-The camera needs to be at an angle turned 90 degrees from the direction of the vector between the two players in order to be facing the side.

Am I on the right track? If so, how can I accomplish my hypothesis?

Here's the script for reference. (It works, I just don't know how to do the final thing I need to do.)

local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable
local player = game.Players.LocalPlayer
local P2 = workspace.P2 --change this


function getDistance(p1, p2)
    return p1.Torso.Position - p2.Torso.Position
end

function updateCamera(camera)
    local distance = getDistance(player.Character, P2)
    local dot = player.Character.Torso.Position:Dot(P2.Torso.Position)
    local magnitude1, magnitude2 = player.Character.Torso.Position.magnitude, P2.Torso.Position.magnitude
    local angle = math.acos(dot/(magnitude1*magnitude2))
    print(math.deg(angle))
    local center = player.Character.Torso.CFrame:lerp(P2.Torso.CFrame, 1/2)
    camera.CoordinateFrame = CFrame.new(center.x, center.y, center.z)
                           * CFrame.Angles(0, -angle, 0)
                           * CFrame.new(0, 0, distance.magnitude*(2/3))
end

game:GetService("RunService").RenderStepped:connect(function()
    updateCamera(cam)
end)

edit: updated the script, it works a little closer to how I want it to now, but still no cigar.

Answer this question