Guest User

Code for Fish Problem

a guest
Jan 3rd, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. --scripted by onlineone22
  2. --the tank should have equivalent y and z values.  otherwise, it will average the values thereof and likely goof up.
  3. --the fish must be rotated such that the x axis is forward and backwards, and the z axis is side to side
  4. --the fish must be on the right side of the aquarium
  5. --The Explorer MUST look as such in order to work properly
  6. --Aquarium
  7. -- -Fish
  8. -- - -Swim
  9. -- -Tank
  10. -- - -Mesh
  11.  
  12. --VARS/SETUP
  13. TweenService = game:GetService("TweenService");
  14.  
  15. tiic = 100;--total increments in cycle (must be an integer above 1)
  16. pbei = 0.05;--pause between each increment
  17. sdmt = tiic * pbei;--start delay max time
  18. --100 * 0.05 = 5 seconds per cycle
  19. --for less lag, I would recommened reducing the total increments and pauses thereof
  20. --for inconsistency, I would change these for each fish slightly
  21.  
  22. ttpc = 4;--tilt times per cycle
  23. maxRot = 30;--maximum rotation of the fish in degrees
  24.  
  25. tweenTime = (tiic * pbei) / ttpc;
  26. fishRotateTween = TweenInfo.new(tweenTime, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 1, true, 0)
  27.  
  28. fish = script.Parent;
  29. aqua = fish.Parent;--aquarium
  30. tank = aqua:WaitForChild("Tank");
  31.  
  32. spacing = 15;--0 to 100 for the percent at which you want the fish to stay away from the middle and edge of the tank
  33. safe = 1 - ((math.random(spacing * 2)) / 100);--number by which to multiply the radius to find the safe space
  34.  
  35. --circumference = pi * diameter
  36. diameter = safe * ((tank.Size.Y + tank.Size.Z) / 2);--(averages x and z)
  37. radius = diameter / 2;
  38. circ = math.pi * diameter;--circumference of the tank
  39. -------cri = 360 / tiic;--circumference rotation increment
  40. cmi = circ / tiic;--circumference movement increment
  41.  
  42. --fish position is a random area in the allowed non-spaced area, starts in the middle of the tank, 0
  43. fish.Position = tank.Position + Vector3.new(radius, 0, 0);
  44. fish.Orientation = fish.Orientation + Vector3.new(maxRot * -1, 0, 0);
  45.  
  46. --FUNCTIONS
  47. SwimInCircle = coroutine.wrap(function()
  48.     while true do
  49.         --rotate fish to face center piece from side, and up and down.  alter rotation.  (x = up and down, y = sideways, z = 0)
  50.         --fish.CFrame = fish.CFrame * CFrame.Angles(0, math.rad(cri), 0)--rotation
  51.        
  52.         --increment fish moving in direction it's facing, staying within the cylindric cycle.  alter x and z position.
  53.         fish.CFrame = fish.CFrame + (fish.CFrame.lookVector * cmi);--forward
  54.        
  55.         wait(pbei);--pause between each recursive loop
  56.     end
  57. end) print(tweenTime)
  58. --SOMETHING WRONG IN ROBLOX' TWEEN FUNCTION, NOT DOING IT CONSISTENTLY
  59. TweenFishTilt = coroutine.wrap(function()
  60.     while true do print(Vector3.new(maxRot, fish.Orientation.Y + (360 / ttpc), 0))
  61.         --reset maxRot in properties to account for negative flip, and updates the rotate value
  62.         local fishRotateProperties = {Orientation = Vector3.new(maxRot, fish.Orientation.Y + (360 / ttpc), 0)};--FIX HERE
  63.         --tweens the fish
  64.         local Tween = TweenService:Create(fish, fishRotateTween, fishRotateProperties);
  65.         Tween:Play();
  66.        
  67.         Tween.Completed:wait();--time between each tilt change, it should be (tiic * pbei) / ttpc but isn't
  68.        
  69.         maxRot = maxRot * -1;--switch the tween setting to set to negative
  70.     end
  71. end)
  72.  
  73. --BODY
  74. wait(math.random(sdmt) - 1);--time to wait after loading before procedure
  75.  
  76. SwimInCircle();
  77.  
  78. wait(tweenTime);
  79.  
  80. TweenFishTilt();
Advertisement
Add Comment
Please, Sign In to add comment