Space Shooter Game: What is a Game Object? Tranform? Component? A Script?!

Welcome to the first article in a series of articles that by the end will have demonstrated how to go about building a 2D space shooter game in Unity!

There is quite a bit of basic stuff to go over before I can really dig into the fun stuff, so instead of skipping over it like I did in my first article I wanted to cover it this time. The goal right now is to get a cube to move around in a scene in Unity.

What is a scene you ask? According to the Unity Documentation

Scenes are where you work with content in Unity. They are assets that contain all or part of a game or application. For example, you might build a simple game in a single scene, while for a more complex game, you might use one scene per level, each with its own environments, characters, obstacles, decorations, and UI
. You can create any number of scenes in a project.

When you create a new project and open it for the first time, Unity opens a sample scene that contains only a Camera
and a Light.”

Unity — Manual: Scenes (unity3d.com)

When you open up a brand new project the default scene is shown. I like to delete it and make a new one. I called mine “Game” in this case.

As you can see, the project starts with a scenes folder in the project window. Disregard the Materials and Scripts folders for now. I’ll get into that later.

Now. I want a cube in my scene. So I right click in the hierarchy window and go to 3D Object > Cube and click that. This puts a cube in the scene. But now what? Your main camera should be able to see it in the game view, but if not, set your camera’s transform position to -10 on the z axis.

WHAT AM I TALKING ABOUT? Click your main camera in the hierarchy, then look at the inspector window.

That is the transform component. This determines the position of your game object. So if you set the z position to -10 on your camera and then zero out the position of your cube it should show the cube right in the middle of the game view.

Change the value of any of the axis in the cube’s transform and you will see it move in the game view.

This is the component I will be accessing via script to move the game object at runtime. To learn more about what a script is check out this link.

Unity — Manual: Scripting (unity3d.com)

I’m going to create a folder in my project called scripts and then right click it and create a new c# script. I want to move the cube around. The cube represents the player in this case, so I’m going to call it Player.

Double click the script and get scared!

jkjk its not as scary as it looks.

Later I’ll dig a little deeper into this entire thing, but for now I’ll cover the Start function and Update function.

private void Start() is a function that makes the code within the brackets run when the application starts. Private void Update() is called every frame. So if you have a game running at 60FPS this function will be called 60 times per frame.

I want to be able to continuously move the player(cube). So the code I want to write will live within the Update(). For now I’m going to go over how to access the transform component of a game object. In this article I will simply set the position of the object in start. When the game starts the cube will move from (0,0,0) to a different position.

Unity has lots of documentation that covers everything you need to make games. Understanding and remembering the syntax is pretty challenging, so I recommend getting familiar with the Unity API documentation.

Here is the script for simply setting the cube to a new place on the screen when you run the game.

In Start() I access the transform of th cube by saying transform and then .position. The “.” allows me to basically access something specific within a component. Its a very hierarchical structure. So if I wanted to acess the rotation parameter of the transform component I’d say transform.rotation.

I set the transform.position equal to a new Vector3. A Vector3 is a representation of points in a 3D space. So my script sets the x position to 0, the y position to 6, and the z position to 0.

Be sure to attach this script to the cube. Run the game and you should see the cube teleport to the position in the script. To verify it, check out the transform of the cube in play mode.

In the next article I’ll go into moving the player at runtime with the WASD keys! Look forward to seeing you there.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store