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

How would I make smooth random motion? (Brownian Motion)

Asked by 6 years ago

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)

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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 :)

0
He probably wants a shaking affect. It probably goes fast and no one knows it does not tween. Also, tweening would just ruin the shaking affect. hiimgoodpack 2009 — 6y
0
Not in this case. I specifically made the code so that the time between each new vibration point is adjustable to a low value, like 0.1. Copy paste the code into a localscript and try it yourself, I think you'll be surprised at how pleasing the effect is... whenallthepigsfly 541 — 6y
0
I do not want a vibration, I need a smooth path to follow. Lord_Hagenost 40 — 6y
0
Sortof like this http://prntscr.com/h2ybei Lord_Hagenost 40 — 6y
View all comments (3 more)
0
In a 2D pattern? Around the player keeping them centered? In a 3D pattern? What sort of motion, specifically, are you trying to achieve? whenallthepigsfly 541 — 6y
0
In a 2D pattern, a smooth line like the one I drew in that screenshot. Lord_Hagenost 40 — 6y
0
Answer is updated, hopefully this is what you're looking for. whenallthepigsfly 541 — 6y
Ad

Answer this question