Advertisement
Animescapetower

Move sun script fixed

Mar 4th, 2018
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. lp=game.Players.LocalPlayer
  2. local Tool = Instance.new('HopperBin',lp.Backpack)
  3. Tool.Name="Don't press 1"
  4. wait(0.5)
  5. Tool.Name="MoveSun"
  6.  
  7. -- convert number (in hours) to TimeOfDay string
  8. -- because TimeOfDay doesn't cast numbers as expected (3.7 -> 03:07:00 instead of 3:42:00)
  9. local function ToTimeOfDay(n)
  10. n = n % 24
  11. local i,f = math.modf(n)
  12. local m = f*60
  13. local mi,mf = math.modf(m)
  14. m = tostring(math.abs(math.floor(m)))
  15. local s = tostring(math.abs(math.floor(mf*60)))
  16. return i..":"..string.rep("0",2-#m)..m..":"..string.rep("0",2-#s)..s
  17. end
  18.  
  19. -- convert TimeOfDay string to number (in hours)
  20. local function FromTimeOfDay(t)
  21. local signed,h,m,s = t:match("^(%-?)(%d+):(%d+):(%d+)$")
  22. s = tonumber(s)/60
  23. m = tonumber(m + s)/60
  24. h = tonumber(h) + m
  25. return h * (#signed > 0 and -1 or 1)
  26. end
  27.  
  28. local function rad_sc(n)
  29. return n/(math.pi*2)
  30. end
  31.  
  32. local function sc_rad(n)
  33. return n*(math.pi*2)
  34. end
  35.  
  36. -- convert direction to latitude (as GeographicLatitude) and longitude (as TimeOfDay)
  37. local function ToLatLon(d)
  38. d = Vector3.new(-d.x,-d.y,d.z) -- derp derp derp derp derp
  39. local lat = math.atan2(d.z,math.sqrt(d.x^2 + d.y^2))
  40. local lon = math.atan2(d.y,d.x)
  41.  
  42. lat = rad_sc(lat)*360 + 23.5
  43. lon = ToTimeOfDay(rad_sc(lon)*24 - 6)
  44.  
  45. return lat,lon
  46. end
  47.  
  48. --[[
  49. -- convert lat and lon to direction (doesn't work)
  50. local function to_dir(lat,lon)
  51. lat = sc_rad((lat - 23.5)/360)
  52. lon = sc_rad((FromTimeOfDay(lon) + 6)/24)
  53.  
  54. return Vector3.new(
  55. (math.cos(lat)*math.cos(lon)),
  56. (math.cos(lat)*math.sin(lon)),
  57. math.sin(lat)
  58. )
  59. end
  60. ]]
  61.  
  62. local Event = {}
  63. local function Disconnect(...)
  64. for _,name in pairs{...} do
  65. if Event[name] then
  66. Event[name]:disconnect()
  67. Event[name] = nil
  68. end
  69. end
  70. end
  71.  
  72. local Lighting = Game:GetService("Lighting")
  73. local down = false
  74.  
  75. local P = 0.02
  76. local D = 16
  77. local position = Lighting:GetSunDirection()
  78. local velocity = Vector3.new(0,0,0)
  79. local goal = Lighting:GetSunDirection()
  80.  
  81. local active = false
  82. local function Activate(Mouse)
  83. position = Lighting:GetSunDirection()
  84. goal = Lighting:GetSunDirection()
  85. active = true
  86. Event.Down = Mouse.Button1Down:connect(function()
  87. down = true
  88. goal = Mouse.UnitRay.Direction
  89. end)
  90.  
  91. Event.Up = Mouse.Button1Up:connect(function()
  92. down = false
  93. end)
  94.  
  95. Event.Move = Mouse.Move:connect(function()
  96. if down then
  97. goal = Mouse.UnitRay.Direction
  98. end
  99. end)
  100.  
  101. asd = game:GetService'RunService'.RenderStepped:connect(function()
  102. velocity = Vector3.new(
  103. velocity.x + P * ((goal.x - position.x) + D * -velocity.x),
  104. velocity.y + P * ((goal.y - position.y) + D * -velocity.y),
  105. velocity.z + P * ((goal.z - position.z) + D * -velocity.z)
  106. )
  107. position = position + velocity
  108. local lat,lon = ToLatLon(position)
  109. Lighting.GeographicLatitude = lat
  110. Lighting.TimeOfDay = lon
  111. --print(lon)
  112. --wait()
  113. end)
  114. end
  115.  
  116. local function Deactivate()
  117. active = false
  118. down = false
  119. asd:disconnect()
  120. Disconnect("Down","Up","Move")
  121. end
  122.  
  123. Tool.Selected:connect(Activate)
  124. Tool.Deselected:connect(Deactivate)
  125. -- convert number (in hours) to TimeOfDay string
  126. -- because TimeOfDay doesn't cast numbers as expected (3.7 -> 03:07:00 instead of 3:42:00)
  127. local function ToTimeOfDay(n)
  128. n = n % 24
  129. local i,f = math.modf(n)
  130. local m = f*60
  131. local mi,mf = math.modf(m)
  132. m = tostring(math.abs(math.floor(m)))
  133. local s = tostring(math.abs(math.floor(mf*60)))
  134. return i..":"..string.rep("0",2-#m)..m..":"..string.rep("0",2-#s)..s
  135. end
  136.  
  137. -- convert TimeOfDay string to number (in hours)
  138. local function FromTimeOfDay(t)
  139. local signed,h,m,s = t:match("^(%-?)(%d+):(%d+):(%d+)$")
  140. s = tonumber(s)/60
  141. m = tonumber(m + s)/60
  142. h = tonumber(h) + m
  143. return h * (#signed > 0 and -1 or 1)
  144. end
  145.  
  146. local function rad_sc(n)
  147. return n/(math.pi*2)
  148. end
  149.  
  150. local function sc_rad(n)
  151. return n*(math.pi*2)
  152. end
  153.  
  154. -- convert direction to latitude (as GeographicLatitude) and longitude (as TimeOfDay)
  155. local function ToLatLon(d)
  156. d = Vector3.new(-d.x,-d.y,d.z) -- derp derp derp derp derp
  157. local lat = math.atan2(d.z,math.sqrt(d.x^2 + d.y^2))
  158. local lon = math.atan2(d.y,d.x)
  159.  
  160. lat = rad_sc(lat)*360 + 23.5
  161. lon = ToTimeOfDay(rad_sc(lon)*24 - 6)
  162.  
  163. return lat,lon
  164. end
  165.  
  166. --[[
  167. -- convert lat and lon to direction (doesn't work)
  168. local function to_dir(lat,lon)
  169. lat = sc_rad((lat - 23.5)/360)
  170. lon = sc_rad((FromTimeOfDay(lon) + 6)/24)
  171.  
  172. return Vector3.new(
  173. (math.cos(lat)*math.cos(lon)),
  174. (math.cos(lat)*math.sin(lon)),
  175. math.sin(lat)
  176. )
  177. end
  178. ]]
  179.  
  180. local Event = {}
  181. local function Disconnect(...)
  182. for _,name in pairs{...} do
  183. if Event[name] then
  184. Event[name]:disconnect()
  185. Event[name] = nil
  186. end
  187. end
  188. end
  189.  
  190. local Lighting = Game:GetService("Lighting")
  191. local down = false
  192.  
  193. local P = 0.02
  194. local D = 16
  195. local position = Lighting:GetSunDirection()
  196. local velocity = Vector3.new(0,0,0)
  197. local goal = Lighting:GetSunDirection()
  198.  
  199. local active = false
  200. local function Activate(Mouse)
  201. position = Lighting:GetSunDirection()
  202. goal = Lighting:GetSunDirection()
  203. active = true
  204. Event.Down = Mouse.Button1Down:connect(function()
  205. down = true
  206. goal = Mouse.UnitRay.Direction
  207. end)
  208.  
  209. Event.Up = Mouse.Button1Up:connect(function()
  210. down = false
  211. end)
  212.  
  213. Event.Move = Mouse.Move:connect(function()
  214. if down then
  215. goal = Mouse.UnitRay.Direction
  216. end
  217. end)
  218.  
  219. asd = game:GetService'RunService'.RenderStepped:connect(function()
  220. velocity = Vector3.new(
  221. velocity.x + P * ((goal.x - position.x) + D * -velocity.x),
  222. velocity.y + P * ((goal.y - position.y) + D * -velocity.y),
  223. velocity.z + P * ((goal.z - position.z) + D * -velocity.z)
  224. )
  225. position = position + velocity
  226. local lat,lon = ToLatLon(position)
  227. Lighting.GeographicLatitude = lat
  228. Lighting.TimeOfDay = lon
  229. --print(lon)
  230. --wait()
  231. end)
  232. end
  233.  
  234. local function Deactivate()
  235. active = false
  236. down = false
  237. asd:disconnect()
  238. Disconnect("Down","Up","Move")
  239. end
  240.  
  241. Tool.Selected:connect(Activate)
  242. Tool.Deselected:connect(Deactivate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement