COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Lab Week 2

Posted by on July 27th, 2010 · Lab

Note: If you missed last week’s lab you will need to activate your CSE account. Look at the instructions from last week. Ask your tutor about this before you proceed.

Your task in this lab will be to create some objects and make them move. You’ll also learn how to import more interesting models than simple cubes and spheres.

Step 1: Add a model

  1. Open up unity and create a new project.
  2. Go to turbosquid.com and find a model you like. It can be anything you want, but make sure it is available in Autodesk FBX format.
  3. Download the model (you will have to create an account with turbosquid if you don’t already have one). Make sure you download the FBX file
  4. Go to Unity and select Assets > Import new asset…. In the file dialog select the FBX file you downloaded.
  5. The model should appear in your Project view. Select it and drag it into the Scene view to create a new object.
  6. Arrange the model and the camera so you can see the model in the Game view. You may also need to add some lights to the scene.

Step 2: Make it move

  1. Download the Move.js script. This is the script we wrote in the Wednesday lecture.
  2. Use Assets > Import new asset… to add this script to your project.
  3. Open the script and take a look at the code.
  4. Add the script to your model by dragging it from the Project view and dropping it on the object in the Hierarchy.
  5. You will be prompted about Losing the prefab. Click Continue.
  6. Select the object and find the Move script in the Inspector. Set some values for the direction vector.
  7. Press Play to run the scene and watch it move. You may need to use the Pause function if it moves too fast.
  8. You can also see it moving in the Scene view and watch its position changing in the Inspector.
  9. Go into the editor and change the Update() function by adding the line:

    Debug.DrawRay(transform.position, direction);

    This method draws a ray in the Scene view starting from the objects position and pointing in ‘direction’. It is useful for debugging. You can read the docs for it here.

  10. Run the scene again and look in the Scene view to see the result (again, use pause and step through).

Change the script

  1. Select the script in the Project view and press command-D to duplicate it.
  2. Rename the duplicate as ‘MoveTowardsTarget’.
  3. Edit the script, adding a new variable:

    var target : GameObject;

  4. Your task is now to figure out how to make the object move slowly towards the target. Work in pairs. Work it out on paper first and then try writing code.
  5. Use the Console view to check for any syntax errors in your code.
  6. Add your new behaviour to your object, set the target and run it. Does it work? (Remember to remove the old script first!)
  7. Experiment. What happens if you have multiple objects tracking each other? What if one object has two different movement scripts?
  8. Can you provide a way to vary the speed of the movement?
  9. Can you make the object move away from its target?
  10. Challenging: Can you make the object orbit its target?

Show your work to your tutor to get a lab mark.

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

You must log in to post a comment.