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

Camera script suddenly broke on me

Asked by 10 years ago

It was working a while ago in my game, but now it suddenly stopped working properly.

WHAT IT IS SUPPOSED TO DO: Block you from zooming out, body and person follows where the mouse it; basically a third person camera that prevents you from zooming in or out, and follows the mouse.

WHAT ITS DOING NOW: It is allowing users to zoom in and out, and the only affect it's doing is the body is facing the wrong way ( hard to explain )

Here is the code

local abs = math.abs

local distance = 5
local zoom = 70 --(20 to 80)
local sensitivity = 0.25
local height = 1

local player =  game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = Workspace.CurrentCamera

while not camera do
    Workspace.Changed:wait()
    camera = Workspace.CurrentCamera
end
while not mouse do
    wait(0.5)
    mouse = player:GetMouse()
end

wait()

local char = true
local gyro, head, torso

camera.CameraType = "Scriptable"
camera.FieldOfView = zoom

while wait() do
    if char ~= player.Character then
        char = player.Character
        while not char do
            char = player.CharacterAdded:wait()
        end
        torso = char:WaitForChild "Torso"
        head = char:WaitForChild "Head"
        gyro = torso:FindFirstChild "lookatmouse" or Instance.new "BodyGyro"
        gyro.maxTorque = Vector3.new(0, 7000, 0)
        gyro.cframe = CFrame.new(0, 0, 1) + torso.Position
        gyro.Name = "lookatmouse"
        gyro.Parent = torso
    end
    local hit = mouse.Hit
    if mouse.Hit.p.magnitude ~= 0 then
        local objhit = torso.CFrame:pointToObjectSpace(hit.p)
        local pos = head.Position + Vector3.new(0, height, 0)
        local focus = CFrame.new(pos)
        local vec
        if abs(objhit.x) > objhit.z and 
            (objhit.x/objhit.y)^2 + (objhit.z/objhit.y)^2 > 1 then
            vec = mouse.Hit.p - pos
        else
            vec = camera.CoordinateFrame.lookVector
        end
        pos = pos - vec.unit * distance
        camera:Interpolate(
            CFrame.new(
                pos,
                vec + pos
            ),
            focus,
            sensitivity
        )
        gyro.cframe = mouse.Hit
    end
end

1 answer

Log in to vote
0
Answered by 10 years ago

I apologize as I meant to reply to this days ago, but I forgot to do so. When I tested your script to get an idea of what you're talking about, it seems to do what you say it should be doing; it's a very smooth moving camera that lets the character follow the mouse and I have no abilities to zoom in or out when in use. My character seems to be facing the correct way. Are you sure you have no other scripts interfering with this one; possibly causing the issues on your character?

Ad

Answer this question