- Home /
aquarium effect for 2d fish
i want simple 2d Ai for my fish ,with little R&D i figure out different type of solution but i dont think that is applied to my case
solution that i found was
1)use of way points(but it my fish will use the same path over and over again which is not god if you have 100 fish and all use one update function each to follow way points
2)use of trigger![alt text][1]
[1]: /storage/temp/28259-triger.png
but here also i can only move my object to same point left to rit
but i want effect like aquarium
Answer by HappyMoo · Jun 26, 2014 at 12:13 AM
Just pick a random target point within the aquarium and use Vector3.MoveTowards with the fish speed to move him towards that point. When he reached it, pick a new target.
For smoother movement, don't move the actual fish with MoveTowards, but another target and Lerp or MoveTowards the fish to this target. In summary: Pick end target. Move current target with MoveTowards till it reached end target, then pick a new end target. Lerp/MoveTowards tthe fish position between current position and current target. This is similar to what musranya is saying, but produces more even speeds, so the fish isn't rushing at the start and get slower later and also produces rounded paths like bezier curves and not just linear segments.
If you wanna go more complex, google swarming and flocking behaviour. You'll probably also find some unity implementations.
Let me know if you have more questions about implementing this solution.
which should be ,fish face will be facing way point. how can i do that ?
$$anonymous$$oving and facing direction are to separate things. I told you how to move. Apart from that, after you calculated which direction you have to move, you can update your rotation... something like this:
fish.transform.rotation = Quaternion.LookRotation(moveDirection, Vector3.up);
Answer by musaranya · Jun 25, 2014 at 01:43 PM
A possible approach would be to create various sets of waypoints (or paths) and then assign the path to each fish randomly. Also you can introduce params when assigning a path to a fish, p.e. variable Lerp velocity between points, an initial position offset, etc.
Another idea taht needs to be tried in order to now if it makes de trick: set points at the corners of the tank and, for the movement of the fish, make a lerp from its initial position to one of these points randomly. After a few secs, change the target point of the Lerp randomly again, and so one. If it works you should have a bunch of fishes swimming around the tank and never reaching the corners
Well, are just a few ideas, anyway hope you can make it :)
Your answer
Follow this Question
Related Questions
Help with 2D AI scripting 2 Answers
2D Platformer Ai Movement Decision Making Problems 0 Answers
Simple AI In 2D - C# 1 Answer
enemy AI patrol not working 1 Answer
moving AI up/down if lower/higher in Sector as player 0 Answers