Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I have an AI Target areas where a noise is made?

Asked by 10 years ago

I need to make a more advanced AI, but I don't know how to go about a few things. I want an AI to be attracted to certain positions based on noise-making, such as a bottle being thrown or a gunshot. But I don't know what to do in this case. Would I have the script create a set of values in the workspace that, if say the sound was made close enough to the ai and the ai isn't already focused on something, would cause the ai to wander around that position?

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

That's a simple way to do it.

If you simply keep track of all of the "sounds" made in the last few seconds, the zombies can track to those instead of the nearest player. If there are no nearby positions to take, just remember the old value, so that it'll keep tracking there (and you can add additional logic to make it wander).

Overall, it could look like this:

function makeNoise(position)
    local p = Instance.new("Vector3Value",workspace.Noises);
    p.Value = position;
    delay(function() p:Destroy(); end,10);
    -- Destroy the noise after ten seconds
    -- (since zombies might not look every moment, we give them
    -- a chance to notice)
end


function findNoise(near,within)
    local nearest = nil;
    for i,v in pairs(workspace.Noises:GetChildren()) do
        if (not nearest) or (nearest - near).magnitude
            >
        (nearest - v.Value).magnitude then
            nearest = v.Value;
        end
    end
    return nearest;
end

local target = myposition;
local vary = Vector3.new();
while true do
    wait();
    -- Other logic
    local noise = findNoise(myposition);
    if noise then
        target = noise;
    end
    if math.random() < .01 then
        vary = Vector3.new(math.random()*50-25,0,math.random()*50-25);
        --simple wandering
    end
    wandertarget = target + vary;
    moveTo(wandertarget);
end
1
Wow thank you very much, this helps me a lot. However I am afraid I don't understand why you put a ";" after most statements. Aaronoles 55 — 10y
0
Semicolons are almost always optional. Many other programming languages require them, and since I switch languages frequently it makes it easier if I just always use them. In addition, it bothers me that those exception lines require semicolons and nothing else, so I like to include them anyway for style purposes. You can choose, it makes no difference, just personal preference. BlueTaslem 18071 — 10y
Ad

Answer this question