Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. workspace.Terrain.WaterWaveSize = .5
  2. workspace.Terrain.WaterWaveSpeed = 15
  3.  
  4. if not _G.GetWaterWaveCycle then
  5. --We have to make sure the water's cycle is 0. There is no way to know this,
  6. --but if the WaterWaveSpeed is nonzero, there's a good chance the cycle is
  7. --nonzero.
  8. --This will not catch cases where the user changes the water wave speed from
  9. --a nonzero value to 0 then calls this code chunk, but I can't think of a way
  10. --to catch that case.
  11. --We just have to hope users use this correctly.
  12.  
  13. local cycleAtLastSet = 0; --The cycle at the time of the last wave speed set.
  14. local tickAtLastSet = 0; --The tick() at the last WaterWaveSpeed set.
  15. local speedAtLastSet = 0; --The last WaterWaveSpeed which was set.
  16.  
  17. --@return A float between 0 and 2pi indicating the current cycle.
  18. --@describe The cycle will be 0 when a place is freshly loaded and the
  19. -- WaterWaveSpeed is 0.
  20. function _G.GetWaterWaveCycle()
  21. return math.pi * 2 * ((cycleAtLastSet + (tick() - tickAtLastSet)
  22. * workspace.Terrain.WaterWaveSpeed / 85) % 1);
  23. end
  24.  
  25. --Listen for changes to WaterWaveSpeed and update the state variables.
  26. workspace.Terrain.Changed:connect(function(prop)
  27. if prop == "WaterWaveSpeed" then
  28. cycleAtLastSet = (cycleAtLastSet +
  29. (tick() - tickAtLastSet) * speedAtLastSet / 85) % 1;
  30. print("Current Cycle State: ", cycleAtLastSet);
  31. tickAtLastSet = tick();
  32. speedAtLastSet = workspace.Terrain.WaterWaveSpeed;
  33. end
  34. end)
  35. end
  36.  
  37. local waveHeight = workspace.Terrain.WaterWaveSize * 2;
  38. --the previous constant 2 is found by trial and error.
  39. --The true value is between 1.5 and 2.5.
  40.  
  41. --This is constant and unchanging.
  42. local waveWidth = 85;
  43.  
  44. --@brief Computes the height of the terrain water at a given location.
  45. --@description The actual y location of the water surface is the return value of this
  46. -- plus the natural resting height of the water (the height at which a block of
  47. -- density 0.5 would float).
  48. --@param x The x location to check.
  49. --@param z The z location to check.
  50. --@param cycle The current water wave cycle. This is a value between 0 and 2pi.
  51. --@return The displayed water surface level minus the physical water surface level.
  52. function height(x, z, cycle)
  53. return waveHeight * math.sin(2*math.pi*x/waveWidth + cycle + math.pi/2)
  54. * math.sin(2*math.pi*z/waveWidth);
  55. end
  56.  
  57. local boat = workspace.Boat
  58. local joints = boat.Joints:GetChildren()
  59. local roots = boat.Chassis:GetChildren()
  60. local FR = boat.Chassis.FR
  61.  
  62. for i, joint in ipairs(joints) do
  63. joint.BodyGyro.CFrame = joint.BodyGyro.CFrame
  64. end
  65.  
  66. game:GetService("RunService").Heartbeat:Connect(function()
  67. local cycle = _G.GetWaterWaveCycle()
  68.  
  69. for i, root in ipairs(roots) do
  70. if(root:FindFirstChild("Connector") and root:FindFirstChild("Connector").Value == true) then
  71. local height = (height(root.WorldPosition.X,root.WorldPosition.Z, cycle))
  72. root.WorldPosition = Vector3.new(root.WorldPosition.X, 4.5+height, root.WorldPosition.Z)
  73. end
  74. end
  75. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement