Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local animcontroller = script.Parent:FindFirstChildOfClass("AnimationController"):WaitForChild("Animator")
- local swim = script.Parent:WaitForChild("Animations"):FindFirstChild("Swim")
- local anim = animcontroller:LoadAnimation(swim)
- local shark = script.Parent
- local primaryPart = shark.PrimaryPart or shark:WaitForChild("HumanoidRootPart")
- local speed = 10
- local chaseSpeed = 17.5
- local chasing = false
- local chaseCooldown = false
- local waypoints = workspace:WaitForChild("SharkWayPoints"):GetChildren()
- table.sort(waypoints, function(a, b) return a.Name < b.Name end)
- local currentWaypointIndex = 1
- local vel = Instance.new("BodyVelocity")
- vel.MaxForce = Vector3.new(10000, 10000, 10000)
- vel.Parent = primaryPart
- anim:Play()
- anim:AdjustSpeed(1)
- local function moveToPosition(targetPosition, moveSpeed)
- local direction = (targetPosition - primaryPart.Position).Unit
- primaryPart.CFrame = CFrame.lookAt(primaryPart.Position, targetPosition) * CFrame.Angles(0, math.rad(180), 0)
- vel.Velocity = direction * moveSpeed
- end
- local function moveToWaypoint()
- if not chasing then
- local target = waypoints[currentWaypointIndex]
- if target then
- moveToPosition(target.Position, speed)
- end
- end
- end
- local function nextWaypoint()
- currentWaypointIndex = currentWaypointIndex + 1
- if currentWaypointIndex > #waypoints then
- currentWaypointIndex = 1
- end
- moveToWaypoint()
- end
- local function chasePlayer(player)
- chasing = true
- local chaseTime = math.random(6, 10)
- local startTime = tick()
- while chasing and tick() - startTime < chaseTime do
- if not player.Character or not player.Character:FindFirstChild("PrimaryPart") or player.Character:FindFirstChild("Humanoid").Health <= 0 then
- break
- end
- local playerPos = player.Character.PrimaryPart.Position
- moveToPosition(playerPos, chaseSpeed)
- anim:AdjustSpeed(1.75)
- task.wait(0.1)
- end
- chasing = false
- chaseCooldown = true
- anim:AdjustSpeed(1)
- moveToWaypoint()
- task.wait(chaseTime)
- chaseCooldown = false
- end
- task.spawn(function()
- while true do
- if not chasing and not chaseCooldown then
- for _, player in pairs(game.Players:GetPlayers()) do
- local character = player.Character
- if character and character:FindFirstChild("PrimaryPart") then
- local distance = (character.PrimaryPart.Position - primaryPart.Position).Magnitude
- if distance <= 30 then
- chasePlayer(player)
- break
- end
- end
- end
- end
- if not chasing then
- local target = waypoints[currentWaypointIndex]
- if target and (primaryPart.Position - target.Position).Magnitude <= 5 then
- nextWaypoint()
- else
- moveToWaypoint()
- end
- end
- task.wait(0.1)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment