Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Orb.ai v1 (Object Targetting) mainframe --
- -- Constants
- local Ball = script.Parent -- Assuming the script is a child of the ball
- local Target = workspace.Target -- Assuming the target is in the workspace
- local CLOSE_ENOUGH = 5 -- Distance to consider the ball close enough to a waypoint
- local MAX_FORCE = math.random(7.5, 8.5) -- Steering force
- local MAX_SPEED = math.random(16, 24) -- Rolling speed
- local AVOIDANCE_FORCE = math.random(0.1, 0.75) -- Force to push the ball away from obstacles
- local LERP_FACTOR = 0.5 -- Rate for speed to change when chaning directions
- local JUMP_FORCE = Vector3.new(0, 50, 0) -- Jump power of the ball (same as players)
- local STUCK_TIME = 2 -- Time (seconds) for the ball to consider as "stuck" if it hasnt moved
- -- Variables to track the ball's position and time
- local lastPosition = Ball.Position
- local lastMoveTime = nil
- -- Function to calculate distance between two points
- local function distance(point1, point2)
- return (point1 - point2).Magnitude
- end
- -- Function to check if a position is obstructed
- local function isObstructed(start, goal)
- local ray = Ray.new(start, (goal - start).Unit * 10)
- local hit, position = workspace:FindPartOnRay(ray)
- return hit and hit:IsA("Part") -- You may need to customize this based on your obstacle structure
- end
- -- Function to apply a steering force towards a target
- local function seek(target)
- local desired = target - Ball.Position
- desired = desired.Unit * MAX_SPEED
- local steer = desired - Ball.Velocity
- steer = steer.Unit * math.min(steer.Magnitude, MAX_FORCE)
- return steer
- end
- -- Function to apply an avoidance force away from nearby obstacles
- local function avoidObstacles(avoidanceDistance)
- local avoidance = Vector3.new(0, 0, 0)
- -- Check for obstacles in all directions
- for angle = 0, 360, 45 do
- local direction = CFrame.Angles(0, math.rad(angle), 0) * Vector3.new(1, 0, 1)
- local position = Ball.Position + direction * avoidanceDistance
- -- If an obstacle is found, add an avoidance force
- if isObstructed(Ball.Position, position) then
- avoidance = avoidance - direction
- end
- end
- return avoidance * AVOIDANCE_FORCE
- end
- -- Function to roll the ball around obstacles
- local function rollAroundObstacles()
- local path = game:GetService("PathfindingService"):CreatePath({
- AgentRadius = Ball.Size.X / 2,
- AgentHeight = Ball.Size.Y,
- AgentCanJump = true,
- AgentJumpHeight = 0,
- })
- path:ComputeAsync(Ball.Position, Target.Position)
- local waypoints = path:GetWaypoints()
- local waypointIndex = 1
- -- Initialize avoidance distance and time to next change
- local avoidanceDistance = math.random(0.5, 3.0) * 2.4 + 0.1 -- Random number between 0.1 and 2.5
- local nextChange = tick() + math.random(5, 10) -- Random time between 5 and 10 seconds in the future
- while waypointIndex <= #waypoints do
- local waypoint = waypoints[waypointIndex]
- -- If the ball is close enough to the waypoint, move to the next one
- if distance(Ball.Position, waypoint.Position) <= CLOSE_ENOUGH then
- waypointIndex = waypointIndex + 1
- else
- -- If it's time to change the avoidance distance, randomize it
- if tick() >= nextChange then
- avoidanceDistance = math.random(0.5, 3.0) * 2.4 + 0.1 -- Random number between 0.1 and 2.5
- nextChange = tick() + math.random(5, 10) -- Random time between 5 and 10 seconds in the future
- end
- -- Apply a steering force towards the waypoint and an avoidance force away from obstacles
- local force = seek(waypoint.Position) + avoidObstacles(avoidanceDistance)
- local desiredVelocity = Ball.Velocity + force
- -- Gradually interpolate between the current velocity and the desired velocity
- Ball.Velocity = Ball.Velocity:Lerp(desiredVelocity, LERP_FACTOR)
- -- If the ball hasn't moved significantly in a while, make it jump
- if lastMoveTime and distance(Ball.Position, lastPosition) < 0.1 and tick() - lastMoveTime > STUCK_TIME then
- Ball.Velocity = Ball.Velocity + JUMP_FORCE + (desiredVelocity * 0.75) -- Proportion of speed while jumping
- end
- -- Update the last position and move time
- lastPosition = Ball.Position
- if not lastMoveTime then
- lastMoveTime = tick()
- elseif distance(Ball.Position, lastPosition) >= 0.1 then
- lastMoveTime = tick()
- end
- end
- wait() -- Adjust the wait time as needed
- end
- -- Stop the ball when it reaches the target
- Ball.Velocity = Vector3.new(0, 0, 0)
- end
- while wait() do
- rollAroundObstacles()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement