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

How to fix my Camera Manipulation script?

Asked by 5 years ago
Edited 5 years ago

Hello,

I'm doing some pretty nasty camera manipulation, where It's supposed to work kinda like this https://i.imgur.com/3WSf9qP.png

The camera is above and behind the player, looking at a point that's in front of the player. When you hold down right click, both the camera and where it's looking at should rotate around the player's center (HumanoidRootPart), just like the default ROBLOX camera.

This is what i've got so far:

wait(0.5)

local Settings = UserSettings()
local GameSettings = Settings.GameSettings
GameSettings.RotationType = Enum.RotationType.MovementRelative

print("Disabling default camera")
script.Parent.CameraScript.Disabled = true -- Disable default camera
print("Default Camera disabled.") 
------------------------------------------------------------------------------
local cplayer = script.Parent.Parent -- current player
local cam = game.Workspace.CurrentCamera -- current camera
local runSvc = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService") -- input service
local m2dn = false -- mouse 2 down

cplayer.Character.Humanoid.AutoRotate = false -- set auto rotate to false

local Player = game.Players.LocalPlayer --get the local player
local Mouse = Player:GetMouse() --get the mouse

local rx=0
local ry=0

Mouse.Button2Down:connect(function()
    m2dn=true
end)

Mouse.Button2Up:connect(function()
    m2dn=false
end)

UserInputService.InputChanged:connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.MouseMovement and m2dn then
        rx=rx+inputObject.Delta.x
        ry=ry+inputObject.Delta.y
    end
end)

local right = false

local function Update()
    local hRootPart = cplayer.Character.HumanoidRootPart -- makes job easier

    local playerPos = hRootPart.Position

    local targetAim = Vector3.new(0,1,-5) -- the offset where the camera aims
    local targetPos = Vector3.new(0,10,5) -- offset of where the actual camera is

    local objCamPos = hRootPart.CFrame:toObjectSpace(cam.CFrame) -- camera position

    local Aim = objCamPos.LookVector -- where the camera is currently aiming
    local Pos = objCamPos.Position -- where the camera is currently at (x,y,z)

    ry = math.clamp(ry,-20,60) -- limit values
    if (rx<-360)then
        rx=0
    elseif (rx>360) then
        rx=0
    end

    local ang = CFrame.Angles(math.rad(ry),math.rad(rx),0) -- calculate angles to rotate by
    targetPos = (ang*CFrame.new(targetPos)).Position -- transform position
    targetAim = (ang*CFrame.new(targetAim)).Position -- transform aim area

    local AimDiff = targetAim-Aim -- difference in where the camera is actually aiming vs where it's supposed to be
    local PosDiff = targetPos-Pos -- same as above but for position

    Aim=Aim+(AimDiff/2) -- smooth values to create a sliding effect
    Pos=Pos+(PosDiff/2)

    Aim = hRootPart.CFrame:toWorldSpace(CFrame.new(Aim)).Position --modify where the camera will be next
    Pos = hRootPart.CFrame:toWorldSpace(CFrame.new(Pos)).Position



    cam.CoordinateFrame = CFrame.new(Pos,Aim) -- actually move the camera
end

runSvc:BindToRenderStep("Camera",Enum.RenderPriority.Camera.Value,Update)

While it does roughly work, there's some issues where it doesn't always rotate around the center point like I want it to.

I've tried changing the way angles are processed in many ways but i've had no success in getting the results I need.

Anyone have any ideas?

Thanks in advance,

dylanpdx

Answer this question