Advertisement
1m1m0

Orb.ai Classic

Feb 3rd, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.45 KB | Source Code | 0 0
  1. -- Orb.ai v1 (Object Targetting) mainframe --
  2.  
  3. -- Constants
  4. local Ball = script.Parent  -- Assuming the script is a child of the ball
  5. local Target = workspace.Target  -- Assuming the target is in the workspace
  6. local CLOSE_ENOUGH = 5  -- Distance to consider the ball close enough to a waypoint
  7. local MAX_FORCE = math.random(7.5, 8.5)  -- Steering force
  8. local MAX_SPEED = math.random(16, 24)  -- Rolling speed
  9. local AVOIDANCE_FORCE = math.random(0.1, 0.75)  -- Force to push the ball away from obstacles
  10. local LERP_FACTOR = 0.5  -- Rate for speed to change when chaning directions
  11. local JUMP_FORCE = Vector3.new(0, 50, 0)  -- Jump power of the ball (same as players)
  12. local STUCK_TIME = 2  -- Time (seconds) for the ball to consider as "stuck" if it hasnt moved
  13.  
  14. -- Variables to track the ball's position and time
  15. local lastPosition = Ball.Position
  16. local lastMoveTime = nil
  17.  
  18. -- Function to calculate distance between two points
  19. local function distance(point1, point2)
  20.     return (point1 - point2).Magnitude
  21. end
  22.  
  23. -- Function to check if a position is obstructed
  24. local function isObstructed(start, goal)
  25.     local ray = Ray.new(start, (goal - start).Unit * 10)
  26.     local hit, position = workspace:FindPartOnRay(ray)
  27.     return hit and hit:IsA("Part")  -- You may need to customize this based on your obstacle structure
  28. end
  29.  
  30. -- Function to apply a steering force towards a target
  31. local function seek(target)
  32.     local desired = target - Ball.Position
  33.     desired = desired.Unit * MAX_SPEED
  34.  
  35.     local steer = desired - Ball.Velocity
  36.     steer = steer.Unit * math.min(steer.Magnitude, MAX_FORCE)
  37.  
  38.     return steer
  39. end
  40.  
  41. -- Function to apply an avoidance force away from nearby obstacles
  42. local function avoidObstacles(avoidanceDistance)
  43.     local avoidance = Vector3.new(0, 0, 0)
  44.  
  45.     -- Check for obstacles in all directions
  46.     for angle = 0, 360, 45 do
  47.         local direction = CFrame.Angles(0, math.rad(angle), 0) * Vector3.new(1, 0, 1)
  48.         local position = Ball.Position + direction * avoidanceDistance
  49.  
  50.         -- If an obstacle is found, add an avoidance force
  51.         if isObstructed(Ball.Position, position) then
  52.             avoidance = avoidance - direction
  53.         end
  54.     end
  55.  
  56.     return avoidance * AVOIDANCE_FORCE
  57. end
  58.  
  59. -- Function to roll the ball around obstacles
  60. local function rollAroundObstacles()
  61.     local path = game:GetService("PathfindingService"):CreatePath({
  62.         AgentRadius = Ball.Size.X / 2,
  63.         AgentHeight = Ball.Size.Y,
  64.         AgentCanJump = true,
  65.         AgentJumpHeight = 0,
  66.     })
  67.  
  68.     path:ComputeAsync(Ball.Position, Target.Position)
  69.  
  70.     local waypoints = path:GetWaypoints()
  71.     local waypointIndex = 1
  72.  
  73.     -- Initialize avoidance distance and time to next change
  74.     local avoidanceDistance = math.random(0.5, 3.0) * 2.4 + 0.1  -- Random number between 0.1 and 2.5
  75.     local nextChange = tick() + math.random(5, 10)  -- Random time between 5 and 10 seconds in the future
  76.  
  77.     while waypointIndex <= #waypoints do
  78.         local waypoint = waypoints[waypointIndex]
  79.  
  80.         -- If the ball is close enough to the waypoint, move to the next one
  81.         if distance(Ball.Position, waypoint.Position) <= CLOSE_ENOUGH then
  82.             waypointIndex = waypointIndex + 1
  83.         else
  84.             -- If it's time to change the avoidance distance, randomize it
  85.             if tick() >= nextChange then
  86.                 avoidanceDistance = math.random(0.5, 3.0) * 2.4 + 0.1  -- Random number between 0.1 and 2.5
  87.                 nextChange = tick() + math.random(5, 10)  -- Random time between 5 and 10 seconds in the future
  88.             end
  89.  
  90.             -- Apply a steering force towards the waypoint and an avoidance force away from obstacles
  91.             local force = seek(waypoint.Position) + avoidObstacles(avoidanceDistance)
  92.             local desiredVelocity = Ball.Velocity + force
  93.  
  94.             -- Gradually interpolate between the current velocity and the desired velocity
  95.             Ball.Velocity = Ball.Velocity:Lerp(desiredVelocity, LERP_FACTOR)
  96.  
  97.             -- If the ball hasn't moved significantly in a while, make it jump
  98.             if lastMoveTime and distance(Ball.Position, lastPosition) < 0.1 and tick() - lastMoveTime > STUCK_TIME then
  99.                 Ball.Velocity = Ball.Velocity + JUMP_FORCE + (desiredVelocity * 0.75) -- Proportion of speed while jumping
  100.             end
  101.  
  102.             -- Update the last position and move time
  103.             lastPosition = Ball.Position
  104.             if not lastMoveTime then
  105.                 lastMoveTime = tick()
  106.             elseif distance(Ball.Position, lastPosition) >= 0.1 then
  107.                 lastMoveTime = tick()
  108.             end
  109.         end
  110.  
  111.         wait()  -- Adjust the wait time as needed
  112.     end
  113.  
  114.     -- Stop the ball when it reaches the target
  115.     Ball.Velocity = Vector3.new(0, 0, 0)
  116. end
  117.  
  118. while wait() do
  119.     rollAroundObstacles()
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement