Hi,
How would I make a player’s camera smoothly face from Pos 1 to Pos 2 using CFrame?
I couldn’t find any existing topics specifically about this so, please do direct me to any relevant topics if you find any.
Hi,
How would I make a player’s camera smoothly face from Pos 1 to Pos 2 using CFrame?
I couldn’t find any existing topics specifically about this so, please do direct me to any relevant topics if you find any.
You could use tween
local Camera = workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
Camera.CFrame = Pos1.CFrame — set the start cframe
local Tween = TweenService:Create(Camera , TweenInfo.new(5) , {CFrame = Pos2.CFrame})
Tween:Play()
Tween.Completed:Wait()
Tween:Destroy()
Woops i miss read the whole thing
local Camera = workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
Camera.CFrame = CFrame.new(Camera.CFrame.Position , Pos1.Position) — set the start cframe
local Tween = TweenService:Create(Camera , TweenInfo.new(5) , {CFrame = CFrame.new(Camera.CFrame.Position , Pos2.Position)})
Tween:Play()
Tween.Completed:Wait()
Tween:Destroy()
the code is untested
How does this work? I’ve never seen it used before.
EDIT: I now see that the TweenInfo.new(5) creates a tweeninfo with 5 being the number time, and the rest of the parameters are assumed. (set to default)
This works, however I’m trying to make the position of the camera also move at the same time. This current code just makes the camera fixed in place.
How would I modify it to make it update the position when it is playing?
you can add up another two parts for it’s location.
The only thing you need to change is the first Camera.CFrame.Position to the start Position
and the second Camera.CFrame.Position to the end Position.
edit : pretty similar to the first code but it’s also changing the rotation and position
I didn’t know you could call :Destroy() on Tween Instances. I’m assuming not destroying them causes memory leaks?
For the longest time i’ve been creating tweens without destroying them later
Tween instances. Tweens are instances. All instances inherit from the Instance base class. Destroy is a method of the Instance class. It may not have a physical object but it is still an instance.
Destroy is intended to disconnect all connections and send an instance to nil (thus the instance is held in memory). This instance, in memory without any connections, is weak. It thus gets picked up by the garbage collector and is properly disposed of.
You do not need to necessarily call destroy on a tween instance, but it’s good practice to call destroy on any instance you no longer require.
I guess I never made the connection that the tween object I saw on the advanced objects bar IS the thing you create via TweenService
Never knew this before, thanks.
the garbage collector only does it’s job when collectgarbage()
is called, correct?
Typically the Tween instance should not be visible on the Advanced Objects window, if I recall correctly. Either it does show up and you’re simply not supposed to use it without TweenService:Create, or your Studio has a higher permission level displayed (via settings - I keep mine at the highest level).
I’m not sure what you mean, can you provide code please?
(Sorry for late reply).
local Camera = workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
local pos0, pos1 = workspace.Part0, workspace.Part1
local face0, face1 = workspace.Target0, workspace.Target1
Camera.CFrame = CFrame.new(pos0.Position , face0.Position) — set the start cframe
local Tween = TweenService:Create(Camera , TweenInfo.new(5) , {CFrame = CFrame.new(pos1.Position , face1.Position)})
Tween:Play()
Tween.Completed:Wait()
Tween:Destroy()
I’m thinking that you wanted a moving camera and also facing certain part.
sorry if I got it wrong.
Part0 and Part1 for Camera Position
Target0 and Target1 for Camera facing direction.
I would really suggest you to use Interpolate.
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
local pos1 = game.Workpsace.pos1
local pos2 = game.Workspace.pos2
camera:Interpolate(pos2, pos1, 3)
wait(0.1)
camera:Interpolate(pos1, pos2, 3)
I have always used Interpolate in an event such as ButtonClick so I don’t know if it would work without an event. The reason that I have made pos1,pos2 or pos2, pos1 is that if I make 2 parameters the same it would just get into the part and everywhere would be dark. 3 at the end is the time.
Thank you for reading and Good Luck
This kinda works but, I want a moving camera that is also facing a certain part.
Actually there’s a function of the camera called :Interpolate(Arg1,Arg2,Arg3) which moves the camera to the first CFrame argument, and then makes it face the second CFrame argument. From there once you switch it back to Custom you can adjust the CFrame to be looking in that direction.
Interpolate( Vector3, --The physical place you want it to be
Vector3, --The physical place you want it to look.
number --The amount of time you want it to take)
Something sort of like this.
local Cam = game.Workspace.CurrentCamera
Cam.CameraType = Enum.CameraType.Scriptable --the function requires the camera to be in scriptable mode to work```
Cam:Interpolate(Vector3.new(0,0,5),Vector3,new(5,10,0),3) -- Makes it go to (0,0,5) and face (5,10,0) in 3 seconds
wait(3)
local Frame = Cam.CFrame
Cam.CameraType = Enum.CameraType.Custom
Cam.CFrame = Frame
Hope this helps! You can find more about this function here.