Help with Custom Camera Movement

Just in advance, thanks for the help everyone!

  1. What do you want to achieve?
    I would like to script smooth camera movement to follow a specific part in my game (it’s labeled as mainBCell or brain cell).
  2. What is the issue?
    Currently, I am CFraming the camera constantly using run service. This results in choppy camera movement.
    Here is a link to the video: https://gyazo.com/4f4acb9da44b62cbde807167882047f5

Here’s the code too if anyone wants to see it:

--Sets camera to the center cell, enables top-down

camera.CFrame = CFrame.new(0, 20, 0) * mainBCell.CFrame * CFrame.Angles(math.rad(-90),math.rad(-90),math.rad(-90))

--Live update Camera

local function onUpdate()
--Note that it locks the orientation, it is a cylinder that looks more like a circle.
mainBCell.Orientation = Vector3.new(0,0,90)
--Height is a variable so I can change the camera height during the game.
camera.CFrame = CFrame.new(0, height, 0) * mainBCell.CFrame * CFrame.Angles(math.rad(-90),math.rad(-270),math.rad(-90))

end

RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value, onUpdate)
  1. What solutions have you tried so far?
    Currently, the part mainBCell is moved using a remote event and a Legacy BodyVelocity (I know, but it runs much smoother with the fixed orientation). I think tweening might work, but how would I go about doing that?

    Another idea of mine is to apply another BodyVelocity (Just go with me here, if you really don’t like my usage, tell me how to use a VectorForce…) However, the server cannot interact with the player’s camera.

Which idea is the best, and how would I go about doing it? Thanks,
Someperson576

P.S. I may be late in replying.

2 Likes

I am really confused about the script you gave us but have you tried using TweenService? Tweenservice is way smoother and I can assure you on that.

1 Like

Ok, I’ll be looking into that. I do have two questions:

1. Do I update the tween’s goal using RunService?

2. Do I run the the tween on the server or the client?

Thanks for the reply,
Someperson576

Tweens should be run on the client, with some exceptions. And you don’t use tweenservice, you use tweenservice:

game:GetService("TweenService")

You could try lerping the camera’s CFrame to the CFrame of the mainBCell. This can make the movement to the mainBCell much smoother.
CFrame | Documentation - Roblox Creator Hub (under Functions)

Thanks! I will be in touch on this thread once I test.

Alright @VegetationBush, I have tested tween service, and it seems to be my preferred option. However, it is not working as I would like it to.

Currently my code looks like this:

local tweenInfo = TweenInfo.new(
	2, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
 
local tween = TweenService:Create(camera, tweenInfo, {CFrame = CFrame.new(0, height, 0) * mainBCell.CFrame * CFrame.Angles(math.rad(-90),math.rad(-90),math.rad(-90))})
 
tween:Play()

How can I get the tween to stop when it reaches the desired position, and how can I tween only the position, not the orientation?

Thanks for all the help,
Someperson576

you used (), you need to use {} for the tween info

So its

TweenInfo.new{}

I changed it to that, but it is giving me an error that reads:
“TweenInfo.new first argument expects a number for time.”

How can I stop the tween when the camera is in the desired position, but start it when the camera is not?

I have no idea if this works. I wrote it in the car. While Tweens are nice, camera:Interpolate is better. Camera | Documentation - Roblox Creator Hub

local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = CFrame.new(0, 20, 0) * mainBCell.CFrame * CFrame.Angles(math.rad(-90), math.rad(-90), math.rad(-90))

RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value, function()
    mainBCell.Orientation = Vector3.new(0, 0 , 90)
    camera:Interpolate(CFrame.new(0, height, 0) * mainBCell.CFrame*CFrame.Angles(math.rad(-90),math.rad(-270),math.rad(-90)), mainBCell.CFrame, 0.1)
end))
2 Likes

This is incorrect, he should be using () not {}

For a test try tweening the camera CFrame to a random part’s CFrame and see if it works and if it does you know its an issue on how your calculating the CFrame.

I tested everything, and camera:Interpolate works! Thank you everyone!

I do have two questions though,

A. What does setting the mainBCell as the camera’s focus do?
B. For future reference with interpolating cframes, what exactly is the number alpha (or fraction alpha).

Thanks for the great help,
Someperon576