Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- workspace.Terrain.WaterWaveSize = .5
- workspace.Terrain.WaterWaveSpeed = 15
- if not _G.GetWaterWaveCycle then
- --We have to make sure the water's cycle is 0. There is no way to know this,
- --but if the WaterWaveSpeed is nonzero, there's a good chance the cycle is
- --nonzero.
- --This will not catch cases where the user changes the water wave speed from
- --a nonzero value to 0 then calls this code chunk, but I can't think of a way
- --to catch that case.
- --We just have to hope users use this correctly.
- local cycleAtLastSet = 0; --The cycle at the time of the last wave speed set.
- local tickAtLastSet = 0; --The tick() at the last WaterWaveSpeed set.
- local speedAtLastSet = 0; --The last WaterWaveSpeed which was set.
- --@return A float between 0 and 2pi indicating the current cycle.
- --@describe The cycle will be 0 when a place is freshly loaded and the
- -- WaterWaveSpeed is 0.
- function _G.GetWaterWaveCycle()
- return math.pi * 2 * ((cycleAtLastSet + (tick() - tickAtLastSet)
- * workspace.Terrain.WaterWaveSpeed / 85) % 1);
- end
- --Listen for changes to WaterWaveSpeed and update the state variables.
- workspace.Terrain.Changed:connect(function(prop)
- if prop == "WaterWaveSpeed" then
- cycleAtLastSet = (cycleAtLastSet +
- (tick() - tickAtLastSet) * speedAtLastSet / 85) % 1;
- print("Current Cycle State: ", cycleAtLastSet);
- tickAtLastSet = tick();
- speedAtLastSet = workspace.Terrain.WaterWaveSpeed;
- end
- end)
- end
- local waveHeight = workspace.Terrain.WaterWaveSize * 2;
- --the previous constant 2 is found by trial and error.
- --The true value is between 1.5 and 2.5.
- --This is constant and unchanging.
- local waveWidth = 85;
- --@brief Computes the height of the terrain water at a given location.
- --@description The actual y location of the water surface is the return value of this
- -- plus the natural resting height of the water (the height at which a block of
- -- density 0.5 would float).
- --@param x The x location to check.
- --@param z The z location to check.
- --@param cycle The current water wave cycle. This is a value between 0 and 2pi.
- --@return The displayed water surface level minus the physical water surface level.
- function height(x, z, cycle)
- return waveHeight * math.sin(2*math.pi*x/waveWidth + cycle + math.pi/2)
- * math.sin(2*math.pi*z/waveWidth);
- end
- local boat = workspace.Boat
- local joints = boat.Joints:GetChildren()
- local roots = boat.Chassis:GetChildren()
- local FR = boat.Chassis.FR
- for i, joint in ipairs(joints) do
- joint.BodyGyro.CFrame = joint.BodyGyro.CFrame
- end
- game:GetService("RunService").Heartbeat:Connect(function()
- local cycle = _G.GetWaterWaveCycle()
- for i, root in ipairs(roots) do
- if(root:FindFirstChild("Connector") and root:FindFirstChild("Connector").Value == true) then
- local height = (height(root.WorldPosition.X,root.WorldPosition.Z, cycle))
- root.WorldPosition = Vector3.new(root.WorldPosition.X, 4.5+height, root.WorldPosition.Z)
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement