I am making a “The Mimic”-styled AI. I want to achieve the effect of the screen zooming in and out to let the player know that they’re being chased by the AI. The problem is though, how should I approach this?
I’ve already made the basics of the ai like patrolling, chasing etc. I tried using values like “ChasingWho” but that didn’t work.
Please help me solve this.
You can get the distance between your position and the position of the monster ?
here is the main function of my ai
function main()
local target = findTarget()
if target then
values.Chasing.Value = true
values.ChasingWho.Value = target
chase(target)
else
values.Chasing.Value = false
values.ChasingWho.Value = nil
wander()
end
end
I already finished the AI, I want to do the chasing effect.
Yes but when the AI is near to you you can do the effect !
lemme just send my script one min.
local hum = script.Parent:WaitForChild("Humanoid")
local root = script.Parent:WaitForChild("HumanoidRootPart")
local sp = require(script.Modules.SimplePath)
local values = script.Values
root:SetNetworkOwner(nil)
local params = {
AgentHeight = hum.HipHeight;
AgentRadius = root.Size.X;
AgentCanJump = false
}
function walkRandomly()
local wpFolder = workspace["NMRINWaypoint"]
local goal = wpFolder:GetChildren()[math.random(1, #wpFolder:GetChildren())]
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(root.Position, goal.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(waypoints) do
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
end
else
print("Path failed")
wait(1)
walkRandomly()
end
end
function findPath(target)
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(root.Position,target.Position)
local waypoints = path:GetWaypoints()
local path2 = sp.new(root.Parent, params)
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(waypoints) do
hum:MoveTo(waypoint.Position)
if checkSight(target) then
repeat
print("Moving directly to the target")
path2:Run(target.Position)
attack(target)
task.wait()
if target == nil or target.Parent == nil then
break
end
until not checkSight(target) or hum.Health < 1 or target.Parent.Humanoid.Health < 1
break
end
end
end
end
function checkSight(target)
local ray = Ray.new(root.Position, (target.Position - root.Position).Unit * 40)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
if hit then
if hit:IsDescendantOf(target.Parent) then
return true
end
end
return false
end
function findTarget()
local dist = 50
local target = nil
local potentialTargets = {}
local seeTargets = {}
for i,v in ipairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local hrp = v:FindFirstChild("HumanoidRootPart")
if human and hrp and v.Name ~= script.Parent.Name then
if (root.Position - hrp.Position).magnitude < dist and human.Health > 0 then
table.insert(potentialTargets,hrp)
end
end
end
if #potentialTargets > 0 then
for i,v in ipairs(potentialTargets) do
if checkSight(v) then
table.insert(seeTargets, v)
elseif #seeTargets == 0 and (root.Position - v.Position).magnitude < dist then
target = v
dist = (root.Position - v.Position).magnitude
end
end
end
if #seeTargets > 0 then
dist = 200
for i,v in ipairs(seeTargets) do
if (root.Position - v.Position).magnitude < dist then
target = v
dist = (root.Position - v.Position).magnitude
end
end
end
return target
end
function attack(target)
if (root.Position - target.Position).magnitude < 5 then
if target.Parent ~= nil then
target.Parent.Humanoid:TakeDamage(25)
end
wait(0.4)
end
end
function getPlayerFromChar(part)
return game:GetService("Players"):GetPlayerFromCharacter(part)
end
function main()
local target = findTarget()
if target then
hum.WalkSpeed = 16
findPath(target)
print(target.Name .. target.Parent.Name)
values.Chasing.Value = true
values.ChasingWho.Value = target.Parent
else
values.Chasing.Value = false
values.ChasingWho.Value = nil
hum.WalkSpeed = 8
walkRandomly()
end
end
while wait() do
if hum.Health < 1 then
break
end
main()
end
task.spawn(function()
values.Chasing.Changed:Connect(function()
if values.Chasing.Value then
game.ReplicatedStorage.Found:FireClient(getPlayerFromChar(values.ChasingWho.Value), true)
else
game.ReplicatedStorage.Found:FireClient(getPlayerFromChar(values.ChasingWho.Value), false)
end
end)
end)
Does it work ?
ignore this : egzegz
No :(( it doesnt work
ignore : wwwwwwwwwwwwwwwwwww
Where is the error so ?
Is it the script you show me before ?
It doesn’t error. It says nothing in the output. When I fire the event, it doesn’t zoom out.
Try to get the difference between the player hrp and the monster hrp and if the result is for exemple <= (10,10,10) or >= (-10,-10,-10) you do the effect !
I hope that was helpful have a nice day !
Have you checked if the magnitude is negative?
No, but I kinda changed the system of zoomin in and zooming out.
I used Attributes instead of Values and it works perfectly.
Thank you all for the help!