As explained in my first post I am responsible for the AI of the prey fish in our mobile game. Fish when with other fish tend to school, they follow a flocking behaviour which is an AI technique that can be simulated in a game. My task the past week was to get one fish type to school with each other.
The basics to the flocking algorithm follows three rules. (Note described in the context of fish (flock)) The first main rule for flocking is to have fish move away from other fish. This is called separation, meaning the fish will not run into each other. The next rule makes sure all the fish move in the same direction and at the same speed as the neighbouring fish. This rule is called alignment by keeping them all together. The final rule makes the fish try and stay near the centre of the flock while applying the seperation rule. This rule is called cohesion.
In the past week I have worked through a tutorial that helped interpret these rules into code. The tutorial as well was working with fish flocking and helped give me an idea how it should be setup by having the rules apply every so often, and not every frame to add a degree of randomness to the algorithm. After following through the tutorial I ran into a problem, as I didn't follow it 100% as I knew for the mobile game we were having it side on so the fish need to travel in the x-direction rather than the z. This caused for the fish to just go around in circles, and when they were near each other, they would increase in speed until they hit the edge of the bound area then just go around in a circle again. To try and diagnose the problem I went through printing logs to say when fish were near each other and what their speed was. I had to decrease the fish amount to 2 (down from 10) as well to see when they actually were near each other, but this immediately highlighted the problem as when they were near, their flock size wasn't increasing, and sometimes when they weren't near it was. There was clearly something wrong with the distance function but it didn't make sense that it shouldn't work. So I readjusted the algorithm so that it was all based off the z-axis just so I could get a basic flock working. This worked.
This left me with a nice little program simulating fish in a 10x10x10 area. It was great, but didn't help the reason why I had a problem before, as the game is side on perspective the Z axis isn't used, meaning fish that are further away or closer than the playing area can't logically be eaten. I already created a cube and turned off the mesh renderer so I could see in the editor the area the fish were bound to. I then decided to make this cube useful by getting the spawning script to read it's dimensions and spawn the fish based on that. This allowed me to limit the z-axis binding the fish to an area of 10x10x2. This provides enough room for the fish to turn around without causing problems of going into each other, and is a small enough area that all fish could logically be eaten in it. Then one final change was to have the goal position for the swim to fish to always set on the z-axis at 0, otherwise if it reaches 1 or -1, it can cause problems for the fish turning. Final results:
My task for the next week is to add different types of fish, and have them school with only their type.