Hello Unity

During this course we will be implementing our design patterns in a project that runs on Unity. This means that before we can get to the fun stuff, we first need to be able to run code in this framework. For our greeting we will make a new component that can move the gameobject it is attached to at the speed of 1 unit per second. To make it a bit more interesting we will exclude movement directions that are parallel to any of the x, y or z axis.

Note: speed means that the movement has to be continuous.

In this task you will learn:

  • How to run your C# code in Unity. (Recommended reading: Update pattern and Game loop in textbook)
  • How to create components.
  • How to access other components. (Recommended reading: Component pattern in textbook)
  • Using Time, Transform and Vector3.

Task requirements:

  • The script changes the transfrom component of an gameobject directly. I.e no use of rigidbodies.
  • The speed of movement is 1 unit per second.
  • The speed of movement does not depend on frame rate.
  • The movement must be not parallel to any of the x, y or z axis. I.e directions (1, 0, 0), (0, 1, 0) and (0, 0, 1) will not receive full points.
  • The object may turn. The direction of movement does not need to be fixed.
  • Export the script as a unity package. To create a unity package go to the top menu bar > Assets > Export Package.
  • Upload the unitypackage to CGLearn.

Step by step walkthrough.

  1. Download and install Unity.
  2. Create a new project when prompted on startup.
  3. Create a GameObject. To have a visible object, use GameObject menu at the top of the screen and select 3D Object > Cube. This will also add a component that is responsible for rendering and a component which gives the game object a visible cube shape.
  4. Select the newly created GameObject from the Hierarchy tab. This will make all the components of the GameObject visible in the inspector tab.
  5. Find the Inspector tab and add a new script component to the GameObject. You can do this using the Add Component button under the already attached components. The type of component should be a New Script. You can also name the script at this point.
  6. Open up the newly created script and use it to move the GameObject at the speed of 1 unit per second. You can get the time passed since last frame from Time.deltaTime. The struct Vector3 will also be helpful when dealing with vectors.
  7. Test your code using the play button at top of Unity Editor.
  8. Verify that the object moves at a correct pace from the inspector tab.
  9. Create a Unity package from your project through Assets  Export Package
  10. Upload the package at the bottom of this page.

Tips:

  • Unity components that implement the update pattern, are extended from the MonoBehavior class.
  • In Unity actors are named GameObjects. This class implements the component pattern and always has a Transform component attached to it.
  • Transform component manages the position, rotation, scale and scene hierarchy for the GameObject.
  • Position, Rotation and Scale of a transform are Vector3-s. Vector3 is a struct, not a class! Keep this in mind when modifying them.
  • A float Time.deltaTime contains the fraction of the second it took to complete the last game loop. Use this to make the movement of GameObject independent from frame rate.
;