Swimming Fish!

I’m in part 3 of the four-part course now (dialog), but went back here because this seems the best place to show the fish (visual polish).

Ok, I found an image online of a Brook Trout. In Blender, I modeled the fish. How? by following a horse tutorial (horse…fish…what’s the difference?).: Make Animals In Blender 3 - The Easy Way - YouTube (Grant Abbit’s animal video). In particular, used the image of the brook trout as a brush and touched up by hand. The seems are not completely invisible, but you can only see them from afar and under the water texture.

I rigged and animated it in Blender, using a Shark skeleton (which is included in Rigify). I figured, close enough. There is only one animation: swim. All it does if bend the fish left and right, then loop. An animator controller blend tree maps speed of swimming with a speed parameter determined by its actual velocity. I didn’t bother with animations for turning, it just keeps “swimming”.

Then I wrote a FishController.cs script to look ahead with a raycast and keep it from going through colliders (to include river banks, invisible fish blockers to keep them from going out to sea, and other fish, which have box colliders on them). When an obstacle is found, it probes random directions to find a good way to turn.

It also has some vertical motion. For water level, there’s no collider just test the height versus water height in level (it’s all one height). Raycast keeps it from going through the bottom of the river.

I had to do some tuning; at first, the fish escaped the terrain and swam into space. That was because my raycast went the direction the fish was facing, and if it were swimming upward, it would cast right out of the water and never see the bank. So, there’s two casts, one in the direction of motion, and one horizontal but otherwise in the direction of motion (and a third straight down to detect the bottom of the river).

Here is the code for your study and perusal.

2 Likes

And…more NPCs!

Yellow lines are waypoint paths (for NPCs, they choose “errands” at random from waypoints rather than follow the lines). Green are the computed paths (using AStar Pathfinding Project, a free and pay version exist, google it for details), and blue is a filtered segment of a path. Box colliders in water are the fish swimming around.

Privacy & Terms