How would I smoothly tween a camera movement to a moving object?
Asked by
5 years ago Edited 5 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):
01 | local startCFrame = CFrame.new((rootPart.CFrame.p + Vector 3. new( 0 , 2 , 0 )))*CFrame.Angles( 0 , math.rad(xAngle), 0 )*CFrame.Angles(math.rad(yAngle), 0 , 0 ) |
03 | local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector 3. new(zoomPos.X,zoomPos.Y,zoomPos.Z)) |
05 | local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector 3. new(zoomPos.X,zoomPos.Y,- 50000 )) |
09 | local tween = tS:Create(cam, TweenInfo.new( 0.5 ), { CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p) } ) |
12 | tween.Completed:Wait() |
16 | cam.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p) |