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

How can I make this tween cleaner?

Asked by 5 years ago

So in my game, when a play is in the lobby, it take's the players camera, makes it overlook a room where there are buttons the user can press to play, view help and so on. I want to create this effect that when the player's camera is in the lobby, the camera moves in random directions a bit. I do this by having two parts, CamPlace and CamTarget, and in have it so the camera is always in the same position as CamPlace and looking at CamTarget. Then, in a loop, I tween CamTarget around, and thus getting the effect I want. I have a small problem though. The script that loops the tweening looks something like this:

local TweenService = game:GetService("TweenService")
local part = script.Parent

while true do wait()
    local tweenInfo = TweenInfo.new( --Creates the info
    2,
    Enum.EasingStyle.Sine,
    Enum.EasingDirection.Out,
    1,
    false,
    0
    )
    local tween = TweenService:Create(part, tweenInfo, position) --Position is defined in the script somewhere else and works like its supposed to
    tween:Play() --Plays
    tween.Completed:Wait() --Waits until it finished, then repeats
end

This script works perfectly, however I have a small problem. After the tween finishes, it snaps the part to where it started. I don't know much about tweening, so here is the first question: When a tween finished, does it reset the part's original position, does it not stay where the tween ended? So like, should I do it like when the tween finishes, it set's the part's position wherever the part was sent to, and then tween from there? Wouldn't it snap the part back to the original position a split second before moving it again, and thus making it look a bit weird?

Ok, second question about how to make this cleaner. First of all, I update the camera to be looking at CamTarget using BindToRenderStep. However, I want this to be happening only when the player is not playing, so my script looks something like this:

local function updateLobbyCam()
    if cam.CameraType = Enum.CameraType.Scriptable then
        cam.CFrame = camCFrame -- cam is defined earlier, as is camCFrame. This sets the camer'as position.
    end
end

game:GetService("RunService"):BindToRenderStep("updateLobbyCam", Enum.RenderPriority.Last.Value, updateLobbyCam)

First of all, is BindToRenderStep needed? Doesn't it get updated too fast this way? Wouldn't it be enough if it got updated either every time CamTarget's CFrame was updated, or just several times per second?

One last thing, in updateLobbyCam, I first check if the player is in the lobby and then I update it. Wouldn't it be more efficient to bind the function when the player gets moved to the lobby, and then unbind it when he/she clicks the play button?

As I said before, I'm relatively new to Tweening, so I could really use some advice for future reference c:

0
yeah, but that tweens the camera in a linear fashion, and as I want Quad or Sine, it wouldn't work. RiskoZoSlovenska 378 — 5y

Answer this question