AI acting weird
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 9
- Joined: Thu Apr 26, 2012 9:07 pm
AI acting weird
I have been working on some AI, it doesn't do much but walk around and change direction every few seconds, but it seems to be acting weird. All of the AI end up near the bottom of the screen, even though it should be random. The random number assigned to "down" it 1. Is there a way that math.random(4) is selecting 1 much more frequently than other numbers? You can try it yourself:
- dreadkillz
- Party member
- Posts: 223
- Joined: Sun Mar 04, 2012 2:04 pm
- Location: USA
Re: AI acting weird
I got an error message for HardonCollider. You should repackage your .love with it?
EDIT: Nevermind got it working just removed the require line for HC
EDIT: Nevermind got it working just removed the require line for HC
Last edited by dreadkillz on Tue Jun 19, 2012 8:24 pm, edited 1 time in total.
- OmarShehata
- Party member
- Posts: 259
- Joined: Tue May 29, 2012 6:46 pm
- Location: Egypt
- Contact:
Re: AI acting weird
Hmm, code seems to be correct.
If you're not happy with it currently you could always add an extra check so that if he's down, he's more likely to go up. And same for all directions.
Where it would keep running "ChooseAnotherDirection()" until the AI is no longer going down.
Or a much simpler way, just not make it go towards a certain direction when it's past a certain limit. Etc... there's a lot of way you can experiment to tweak the AI's performance.
If you're not happy with it currently you could always add an extra check so that if he's down, he's more likely to go up. And same for all directions.
Code: Select all
if AiIsGoingDown then
local normalized = (ai.y - (screenHeight/2)) / (screenHeight/2) --Gets a value between 0 and 1. 0 means least down (in the center) and 1 is most down, bottom
if math.random() <= normalized then --As the "normalized" gets bigger, the more likely this check will be true
ChooseAnotherDirection()
end
end
Or a much simpler way, just not make it go towards a certain direction when it's past a certain limit. Etc... there's a lot of way you can experiment to tweak the AI's performance.
- dreadkillz
- Party member
- Posts: 223
- Joined: Sun Mar 04, 2012 2:04 pm
- Location: USA
Re: AI acting weird
I think I figured out what's wrong with your code. You should do this:
You have math.random(4) which chooses a number between 1 and 4 so your AI never actually go up because up is 0.
Code: Select all
math.random(0,3) -- choose between [0,3]
- OmarShehata
- Party member
- Posts: 259
- Joined: Tue May 29, 2012 6:46 pm
- Location: Egypt
- Contact:
Re: AI acting weird
Boy do I feel stupid for missing that.dreadkillz wrote:I think I figured out what's wrong with your code. You should do this:
You have math.random(4) which chooses a number between 1 and 4 so your AI never actually go up because up is 0.Code: Select all
math.random(0,3) -- choose between [0,3]
But this is why I always do math.random() * 4 instead, and round it if needed.
Who is online
Users browsing this forum: Bing [Bot] and 2 guests