Advertisement
1m1m0

Orb.ai Player Tracking

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