Unity Development — Coroutines

Siddhant Thakur
CodeX
Published in
4 min readApr 2, 2021

--

We’ve seen how to use Variables, move an object, how collisions and triggers work with unity and even Coroutines.

Let’s put all that knowledge together and make a simple Auto door system.

Before we proceed further lets take a brief look at Coroutines.

Coroutines :

Think of them as a special function that lets you pause/extend the execution of your code block. Any routine work or anything that you wish to execute over time, that is when you would use Coroutines. For example, Day and Night cycle, it always happens so it can be assumed to be a routine work or a stealth game where an enemy has spotted you and now he pauses everything that he was doing and moves to spot where he last saw you, here the act of stopping whatever he was doing (patrolling maybe) could be done using coroutines. So you can assume that whenever there’s a requirement that you’ll need to do something over a period of time or given an event where you do something that requires a break or an event that breaks the flow, Coroutines can be a safe option to use.

Let’s work on making an Auto Door system, where the door opens automatically when the player approaches the door and will close after entering it.

Here the act of opening and closing a door can be considered as a routine, so we can use Coroutines to implement it.

Let’s start with movement,

where, UserInput() returns a normalized vector3 of our inputs. Clamp is used to create player bounds.

With movement done, let's move onto creating a Trigger zone where upon player contact it triggers an event.

Here Tigger is an empty game object which contains a Box Collider with Is Trigger toggled ON, and a behavior script which triggers our Coroutine.

With this done let’s check for collision, this can be done by,

Here, OnTriggerEnter() is called when we enter the trigger zone, and OnTriggerExit() for when we exit the zone. “other” stores the collider of the object that collided with our game object. GetComponent() is used to fetch the DoorClose() present in our Trigger Event (Script).

Now let’s look at the Coroutine that makes this possible,

The idea here is, that upon triggering our Event to open/close the door. We shall shrink/expand the door, hence the use of while loop as we decrement or increment our local scale of the object. But if this was a normal method the entire process would have been completed in a single frame, which is not what we wanted. Thus, we use Coroutines which let us use the yield return statements, where null means we as the while loop to wait till the end of the frame before continuing. This gives us a gradual and consistent feel of the door opening/closing.

StartCoroutine() is self explanatory.

You can check out my previous articles for more details on the topics covered.

Finally a small trick to make one sided scaling objects…

This is what usually happens when we scale an object along an axis

So this can be achieved by creating an empty object, resetting it’s Transform followed by parenting it with our desired object and setting it to our desired position, For example if we wish to scale the object along the positive x direction we can set the transform to 0.5 and in case we wished to scale it in a negative direction we can do so by setting its transform to -0.5. (0.5 as our cube object is of scale 1).

More to come!!!!!!

--

--

Siddhant Thakur
CodeX

Aspiring Game Developer with experience in the field of Machine Learning.