Details on the Unity environment and how to run it

In this page we will describe the Unity project that you will use and update for Assignment 1.

 

A video clip of what it might look like running the project is found below

Play media comment.

Downloading and running the unity project

  • Download the zip-file (Assignment_1.zip Download Assignment_1.zip) and extract it.
  • Download Unity
  • Start Unity
    • First you get to the "Unity Hub" (new version has a slightly different look, but the idea is the same)
    • unityHub.png
    • Go to "Installs" / "Add" choose the version "2020.3.25f1 LST" (note sure if the last digit "1" matter, but I will stick to this one)
    • Go to "Projects" / "Add" choose the top directory of the extracted zip-file
    • For the new project, choose the proper unity version (listed above) by clicking the small grey triangle
    • Start Unity by clicking the new project
  • Your main window should look something like the following
  • UnityStartScreen.png
  • By pressing the "Play icon" the scene runs, and you can control the car with the arrow buttons

Changing Terrains

  • You can change between the different terrains (TerrainA, TerrainB, TerrainC) specified in the corresponding json-files (Assignment_1/Assets/Resources/Text/TerrainA.json etc)
  • Mark the TerrainManager to the left, and then edit the text field "Terrain filename" to the right.
  • ChoosingTerrain.png

Saving Car Trajectories

  • Any trajectory can be saved using the script TrajectoryLogger.cs, as seen below
  • MainCarLogger.png
  • When "Recording On" is ticked, a json-file is created and saved to Assignment_1/Assets/Resources/Text/Traj3_44_10PM.json, where the numbers are given by the current time (e.g. 15h44m10s = 3:44:10pm) 

Replaying Car Trajectories

  • A trajectory that was saved as above can be replayed using a "ReplayCar" game object as follows
  • ReplayCar.png

Writing your AI code to control the car

  • Each script attached to a game object can be turned off and on
  • By turning off the "Car User Control" script and turning on the "Car AI" script, you disable the arrow keys and turn the control of the car over to the AI script (see below)
  • CarAI.png
  • Running the scene now shows the car making skidding sharp turns (max acceleration and max steering angle). If the Car object is marked the scene view shows a white line drawn from the car to the center of the current grid of the car. This is to illustrate how you can access the grid representation of the world from the script.
  • Besides accessing the map, you can also directly sense the obstacles around the car using a Raycast, simulating a laser range finder. This is illustrated by the yellow line drawn from the car to whatever obstacle is right in front of it.
  • The scene view also shows the planned path in red line segments. Right now this path is a list of random points (see code below). It is your job to do something more clever, both in terms of planning the path, and then controlling the car based on this plan. 
  • Finally, there is a CarAI_PD_tracker.cs script attached to the car. It implements a PD-controller Links to an external site. to track another gameObject called My_target. You might use this as inspiration if you have planned a trajectory and want to track it.
  • CarAIcode.png
  • You will probably write code that runs before the "game" starts running, in "Start()" under "// Plan your path here" as well as code that is regularly called when the game "plays", in "FixedUpdate()" under "// Execute your path here".
  • The complete obstacle matrix is found in "terrain_manager.myInfo.traversability[i,j]" which is a 2d array of floats with 0 for empty and 1 for obstacle. As seen above, there are functions for converting between position coordinates and grid indices. Note that in Unity the y-coordinate is pointing upwards so (x,0,z) is a point in the ground plane.
  • The code for the simulated laser range finder is also included. If you want more "sensors" you can do more raycasts, but overdoing it might give performance problems.

Doing the same thing for the Drone

  • To switch to the drone problem, go to Scenes, and double click the scene called "Main(drone)" (if you want to switch back to the car just double click "Main(car)".
  • This problem is identical in terms of terrain etc., but features a drone instead.
  • First try controlling it manually, using the arrow keys (with the script "Drone User Control" clicked active. The controls apply acceleration in the xz-plane. It is assumed that the altitude of the drone is fixed, so the drone has to pass the obstacles in the same way as the car (but with different dynamics).
  • Note that the drone stops at the wall, but is damaged in the sense that the max acceleration is cut in half (this is to prevent strategies that use walls for slowing down). The lower the max acceleration is, the harder it is to control the drone.
  • Your task, just as in the car case, is to update the "Drone AI" script to plan and execute a trajectory that minimises time to goal.
  • drone_scene.png