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

How would I smoothly tween a camera movement to a moving object?

Asked by 4 years ago
Edited 4 years ago

Issue: So I'm currently working on a custom gun system for a group that I work for. The guns are in over the shoulder third person. When you aim, it smoothly tweens the camera toward the player. My issue is that if you move while the tween is taking place, the camera has to teleport to your location. I'd rather have a solution that does not require stopping the player's movement as that would be annoying in the middle of a fight.

What I have

What I'd like to see

Part of the script in charge of tweening the camera (Ran in RunService.Heartbeat):

local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)

local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(zoomPos.X,zoomPos.Y,zoomPos.Z))
--zoomPos is the vector3 offset
local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(zoomPos.X,zoomPos.Y,-50000))

if prevState ~= 2 then --state 2 is aiming or scoping

    local tween = tS:Create(cam, TweenInfo.new(0.5), {CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)})
    tween:Play()

    tween.Completed:Wait()

else

    cam.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)

end

1 answer

Log in to vote
0
Answered by 4 years ago

I'm dumb and didn't use field of view to zoom the camera in, for anyone wanting to zoom in towards the player it's best just to tween the field of view

Ad

Answer this question