As predicted, there were some things to add to the plan I intended to follow, but again – having guidelines was very helpful.
Since my last update, I got the organisms to move randomly and reproduce.
This was actually a bit harder than expected: I thought I could just add a method that would instantiate a new game object when there’s a collision between two well-fed organisms. But when I tested my code, it made Unity freeze. The problem was, even though I thought the method would only be called once because I called it in OnCollisionEnter(), it was called every frame that two organisms were touching, for every organism that popped up in the same place. As you can imagine, Unity didn’t feel like instantiating that much game objects at the same time.
I therefore added a timer and the problem was solved. However, I had two children pop up each time well-fed organisms touched, because each one called the reproduction method. I came up with a nice solution after I spent some time trying to avoid this in a complicated and inefficient way: I gave each circle an ID, and the reproduction method was called only if the ID of the current circle was lower than the ID of the one he collided with. That way, I could make sure only one of them would ask for reproduction and only one baby would pop up.
To get the circles to move randomly, I put on each one of them a script that chooses a random destination and gets the game object to go straight to it. When one destination is reached, another one is chosen, over and over again. I didn’t write some fancy path-finding code that would take into account other circles, I opted for the straight line and I’m happy with the results – the organisms are supposed to be like protozoans, who don’t think and therefore don’t care if they run into each other.
Here’s a video of the result:
The circles that you see moving quickly are those I control with my finger.
If you didn’t notice, when the game’s over there’s a message saying to “tap to replay” – that’s why in the video the game restarts a few times (and sometimes very quickly).
Next steps: adding a menu and UI and counting points! I’ll give you an update after I’ve finished those things.
A girl in tech.