Intro Unity
Intro Unity
Beste F. Yuksel
Some figures taken from Unity’s award winning tutorials - rest of my own making :)
What is Unity
Unity is a 2D and 3D game engine that can be applied to a variety of platforms
including phones and VR/AR (XR) devices.
A game engine is a framework that allows you to import art and assets (2D and
3D) from other software such as Maya, assemble those assets into scenes, add
lighting, audio, physics, animation, interactivity, and logic.
The scripting language that we will be using with Unity is C# (very similar to Java)
but JavaScript and Boo can also be used.
Unity has fantastic tutorials and documentation which I strongly recommend you
peruse.
Create a new project in Unity called SampleProject (3D)
Let’s import a plane GameObject
If you click on the Plane object in the Hierarchy on
the left, you can view its components on the right.
You can always reset a GameObject’s
position in the same to 0,0,0 by going to
the toggle next to Transform and Reset
GameObjects
Game objects are the items that make up your Unity scenes.
Everything within a scene is considered a game object and these objects are made
up of a number of components.
Each has a necessary transform component that contains the position, rotation
and scale of the game object.
You can view each game object’s components by clicking on it in the Hierarchy.
The Hierarchy is a list of all the game objects in the scene.
Y axis points up
You can alter the Transform.position to move a GameObject or get this value to
locate the GameObject in the 3D world space.
Vector3
Vector3 is a structure (similar to a class) that represents 3D vectors and points.
components.
in Unity.
We can also have 3D vectors
as we saw before as
represented by a Vector3
object in Unity.
Therefore, we can lift the object up by half a unit in the Y axis to lay it on top of the plane.
Script Components
We are now ready to add a script component to the Cube GameObject.
Inspector panel.
Go into your scripts folder and create a C# script.
Call it ‘ObjectMovement’
You will see a preview of the script if you
select it but this is not editable.
}
}
The method Update() is important. It is called before rendering each frame.
We will now add some code to move the cube each frame inside this method.
transform.Translate(Vector3.up * Time.deltaTime);
transform.Translate will translate the position of the object.
Press Play!
Time.deltaTime allows you to move the object a n units per second rather than per
frame (i.e. it slows it down - try the code without it and see)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
}
}
We can also control the speed using a public variable that we can then edit in the
Inspector.
}
}
Now go back to your Inspector
and you will see that the variable
move speed has appeared.
You will actually see the cube move away from you because the camera is
positioned back along the z axis.
You can have the cube move backward along the z axis with this piece of code:
}
}
Object Rotation
Now let’s have the cube rotate. transform.Rotate will rotate the object around the
X, Y, and Z axes, measured in degrees.
The variable turnSpeed is also public (see next slide) and you can control the
speed at which the object rotates.
You can rotate in the other direction by negating the turnSpeed like this:
}
}
Physics - RigidBody
If we want our GameObjects to behave like real world objects such as coming
back to earth with gravity or colliding with other objects, this requires physics.
Create your new Cube and give it a yellow color. Put it near your first cube.
With your CubeRigidBody selected, go to ‘Component’ -> Physics -> Rigidbody.
You will see this new component in the Inspector with ‘Use Gravity’ checked.
Now, let’s edit our script back to the object moving backward along the z axis:
Now Play!
Due to the laws of the physics, when the rigidbody cube falls of the plane it drops
over the edge!
Now change the code so that the cubes move upward and see what happens!
Change the speeds and play it again.
To Explore On Your Own
1. You can learn about the RigidBody class and how to apply force to the
RigidBody (RigidBody.AddForce) in the Roll A Ball tutorial, specifically on ‘Moving
the Player’
Try applying the code in that tutorial to move a GameObject. (It’s a great tutorial!)
2. Have a look at moving the camera position here ‘Moving the Camera’. This will
also give you another way of moving an object. (Another great tutorial!)
Please familiarize yourself with Unity a bit as the Lab 2 won’t be identical to what
we did, but this prep does introduce you to the concepts.