Random&Anime — UnityGameDebut

Showing Randomly & Animation Timing— Episode #07

J3
KidsTronics
3 min readMar 4, 2018

--

Hi, in the last post we get the enemies moving from left to right only once and at the same time (check for the previous code in Github). This is because we put Activate() at the Soldier.Start().

But now let’s add logic for a random shooting routine. For this let’s put Activate() at the EnemyController.Update(). That makes more sense as each soldier is controlled by someone else, like a sergeant or a troop commander right?

Step #01 —Iterate over an array of Soldier: for each Soldier named enemy in the Enemies list call the Soldier.Activate();

Let’s add this code fragment to the Update() of EnemyController.cs (set public to Soldier.Activate()):

Use Awake() instead of Start() in the Soldier.cs so that the Soldier is initialized before the EnemyController.cs is called; Save everything;

Step #02 — To make soldier appears randomly add this to EnemyController.Update() right after the for each routine;

note: we must check if the soldier is currently active (shooting) so add TODO item;

Step #03 — Add this code to the Soldier.cs; it will flag the status of (de)activation of each enemy; The enemy starts to shoot right at the start Activate() and must stop shooting at the end of MoveRightward() when he disappears from the sniper vision; Save Everything;

Step #04 — Add a property called IsActive to Soldier.cs; this is for getter purpose operation;

Step #05 —Call the Soldier.MoveRightwards() right after the iTween.Move(mEnemy, enemyPos, LeftTime); but here’s the Problem: When to call this method? we must know how long it takes to the iTween.MoveTo(mEnemy, enemyPos, LeftTime) returns; for this use right below the second overload method of MoveTo() with a hashtable parameter where we can register a method that is called when the animation is finished; comment mIsActive = false;

Step #06 —Declare a new method OnLeftComplete() that will be called after the MoveLeftward(); so this is the exact time to deactivate the enemy, right?

Step #07— Confirm that the for each loop to the EnemyController.Update() are like this so that it will occur indefinitely;

That’s all for now!

We accomplish a great task: Now the enemies are appearing and ready to shoot randomly…

Here is the complete code till now:

Now shooting time!

But this will be a task for the next post!

Thank You! Bye for now!

Thanks for your visit! Stay tuned!

# 0 #1 #2 #3 #4 #5 #6 #7 #8 #9 #10

GitHub Repo

References & Credits

Making a Game with Unity (1–10) : Basics by Jayanam

--

--

J3
KidsTronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️