I'm trying to create a smooth random path for a camera, but when I use math.random it appears to vibrate around rather than a smooth random motion. Suggestions?
local toCF = function(Vec3) return CFrame.new(Vec3.X,Vec3.Y,Vec3.Z) --To return a CFrame with no rotation end game:GetService("RunService"):BindToRenderStep("BeforeCamera", Enum.RenderPriority.Camera.Value-1, function() local Move = Vector3.new(math.random(-500,500)/250,math.random(-500,500)/250,0) VortexPart.CFrame = VortexPart.CFrame + Move local Pos = VortexPart.CFrame.p Camera.setCFrame(toCF(Pos)) end)
Now that I actually know what you want, I can suggest how you might achieve the motion you're looking for. You might consider slowly rotating a vector by a small constant randomly generated amount (>.5 degree per step) relative to it's last rotation and then moving in the direction of that new vector by another constant random amount, repeating for a random amount of time, or maybe until you reach a predetermined angle. What this would ideally do is trace a random many-sided polygon, essentially a circle. Then, all you'd have to do is interpolate the camera between them. While this still wouldn't be perfectle smooth random motion, anything better would probably require the use of customly generated Catmull-Rom splines. It would also be a good idea to have an origin CFrame so that you could position the 2d plane without much trouble, generating the path on it's XY plane, and then using CFrame:vectorToWorldSpace(v3) to actually get a value to set to the camera. Something to this effect would give you the smooth, kind of circular motion you want, but it won't be super easy to program, so it's up to you to figure out. Good luck!
Original Answer:
You might want to try somthing like this:
local TimePerVibration = .1 --How long each change takes local tweenService = game:GetService("TweenService") camera = workspace.CurrentCamera local VortexPart = Instance.new("Part", workspace) local toCF = function(Vec3) return CFrame.new(Vec3.X,Vec3.Y,Vec3.Z) --To return a CFrame with no rotation end local currentTween game:GetService("RunService"):BindToRenderStep("BeforeCamera", Enum.RenderPriority.Camera.Value-1, function() local Move = Vector3.new(math.random(-500,500)/250,math.random(-500,500)/250,0) VortexPart.CFrame = VortexPart.CFrame + Move local Pos = VortexPart.CFrame.p if not currentTween then currentTween= tweenService:Create(camera,TweenInfo.new(TimePerVibration, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),{ CFrame = toCF(Pos) }) elseif currentTween and currentTween.Completed then currentTween= tweenService:Create(camera,TweenInfo.new(TimePerVibration, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),{ CFrame = toCF(Pos) }) currentTween:Play() end end)
All the code does is check if a previously created tween has finished, and if so tweens the camera to the point you had it going to. It won't be as shaky as VotrexPart, but also not quite so nausea inducing. Enjoy! If you want to read up more.
If my answer helped you, be sure to mark it as correct :)