The simplicity of coding in Unity

Vlad Cojocaru
METRO SYSTEMS Romania
4 min readSep 5, 2019

If you’ve ever wanted to start writing an application without prior knowledge of coding in that particular framework, you most probably have shared my frustration with incomplete tutorials and poor documentations.

All those wasted hours trying to figure out how to implement a simple feature…

When I first started with Unity, I was amazed at just how much you can even achieve in Unity without writing a single line of code.

The interface seems to be already prepared with the most common requirements in mind.

We started a project that required writing an AR application. The first features required were regarding tracking an object and displaying graphics/animations over that particular object.

We managed to implement this in under 40 minutes!

At this point I could also deploy this simple program on most major platforms without any change. Oh and again at this point we did not write a single line of code!

So let’s talk a little bit about this interface. If you’ve used any 3D editing software you’ll most likely feel right at home.

Most options are where you expect them. Moving, rotating or resizing an object is intuitive and you’ll get the handle of it in the first few minutes.

The second relevant piece of the interface is the hierarchy view, where all objects are represented in a very OOP way.

This makes coding for them easy to understand and implement as the language that Unity uses is C#.

Objects themselves can be modified by adding pre-built components to them that may already save you hours of coding.

Let’s say, for example, that I have a scene that contains a ball and a piece of ground:

Now I want the ball to fall and bounce on the floor.

If I wanted to do this using code I would have to implement: gravity, collisions, mass, acceleration. No easy feat and even then it would probably not work on the first try.

What we can do here instead is use the framework’s built in physics system.

To get the ball to act like a ball all we need to do is add the properties RigidBody and Sphere Collider to it and a Box collider to the floor:

This just saved us an afternoon of coding!

Now I know these systems are far from perfect and that you can write better systems if you really want to.

However my point is that you can use systems like this when you’re just starting off. When you are in an exploration phase or in a proof of concept phase.

This helps you focus on what is important for you in your application and delegates less critical tasks to the framework.

So let’s say you reached the point where you would want to start writing some code.

Thankfully in this area, Unity has provided several video tutorials that easily get you accommodated to the ins and outs of their APIs while also familiarizing you with C# basics.

Apart from that, since the framework has gained a lot of popularity, you can find a lot of code snippets for everything from character movement, dynamic interfaces to even more complex topics like procedural object generation.

The systems I’ve talked about before also have APIs so you can always alter their behavior dynamically.

For another example, let’s say you want to make a character that moves in your unity scene. This code would be sufficient:

public void Update() // execute the contents of this method every frame

{
Move(); // call the movement method
}

public void Move()

{

Vector3 Movement = new Vector3 (Input.GetAxis(“Horizontal”), 0, Input.GetAxis(“Vertical”); // Get Horizontal and Vertical movement data from keystrokes or controller data

this.transform.position += Movement * speed * Time.DeltaTime; // apply it to the object that the script is attached to

}

Seems simple enough, right?

This is the true power of the Unity framework: You work on the idea, not the syntax!

So if you’re ever thinking “I can’t possibly write an Augmented Reality app.” , think again. It’s simpler and way more entertaining than you would assume to get to something like this:

--

--

Vlad Cojocaru
METRO SYSTEMS Romania

Software engineer for Metro Systems Romania, Augmented Reality enthusiast.