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. The screenshots are from 2024 and will thus say 2024 where it in this year should say 2025. Furthermore, the look of the GUI looks a bit different, but the overall process is the same.

 

A video clip of what it might look like running the project is found below (the clip shows how to activate and de-activate the carAI script, which at this point does full gas with full right which is not that useful, your job will be to replace it with something better, the clip also shows how to activate the PD-tracker, but you can ignore this for now).

 

Downloading and running the unity project

  • Download and install Unity (pick the right version, as described here)
  • Download the files
    • Get them from https://gits-15.sys.kth.se/DD2438/MAS2025-Assignment-1
    • Create a fork of the repo, where you can then add your own updates
      • Creating a fork is important since that makes the process traceable in case someone is slacking or copying code
    • Also download the related Unity Assets package
    • Put the two file-trees (MAS2025-Assignment-1 and MASUnityAssets) as siblings in a common folder.
  • 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 "Unity 6 (6000.0.32f1) LTS" (note sure if the last digit "1" matter, but I will stick to this one)
    • Go to "Projects" / "Add"/"Add project from disk" choose the top directory of the cloned git repository (MAS2025-Assignment-1, that you got from the link above)
    • Start Unity by clicking the new project
  • Your main window should look something like the following
  • Screen Shot 2025-01-13 at 19.58.43.png
  • The layout of all tabs is reconfigurable so it might be a bit different.
  • First you might have to go find the "Project" tab, choose "Assets/Scenes" and double click "main (car)".
  • Then press the Play-icon in the top. Now you can control the red car with the arrow buttons (you might have to find/click the "game" tab first).

Changing Maps

  • You can change between the different Maps (TerrainA, TerrainB, TerrainC, etc) by clicking the Map object on the left (see screenshot below).
  • To Load a map, pick one of the options in the drop down meny on the right (saying TerrainA below)
  • Then click "Load" right below the dropdown menu.
  • You can also save a map (after editing) by writing a filename (where it says terrainD below), and clicking "Save". But avoid overwriting the existing maps.
  • Screen Shot 2025-01-13 at 19.44.36.png
  • Now if you click the play-button (top middle) you will run in the chosen map.

Saving Car Trajectories

  • Any trajectory can be saved using the script TrajectoryLogger.cs, as seen below
  • loggingTrajectory.png
  • To make changes to the main car, access it through "Assets/Prefabs/Car"
  • Enable (tick) the Trajectory Logger script, and enable "Recording On"
  • Starting the simulation now creates a json-file in "Assets/Resources/Text" named "TrajX_X_X.json" where X is the current time (in AM/PM format, i.e., Traj3_44_10PM.json, corresponds to 15h44m10s = 3:44:10pm).

Replaying Car Trajectories

  • A trajectory that was saved as above can be replayed using a "ReplayCar" game object as follows.
  • Activate the "Mass Replay Manager" script, inside the Map object (below the place where you load/save maps described above).
  • When pressing play, a CarReplay object will be created for each saved trajectory in the folder, that starts with the given keyword (currently "Traj...").
  • Note that if the trajectory was recorded using another map, the trajectory might go though obstacles, no collision checking is done for replays.

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)
  • carScripts.png
  • Running the scene now shows the car making skidding sharp turns (max acceleration and max steering angle). 
  • In the scripts there are several features to help you access the map (see below).
  • 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.
  • Finally, there is a CarAI_PD_tracker.cs script attached to the car. It implements a PD-controller Links to an external site.. It can be used to either track another gameObject called My_target. Or to perform circles of a given radius and speed. You might use this as inspiration if you have planned a trajectory and want to track it. The circle tracking makes it easier to figure out how fast the car can turn at different speeds, without loosing traction.
  • You encouraged to write your code in the scrips "CarAI.cs".
  • By double-clicking a script in the Unity engine, it opens up in Visual Studio (or some other editor you use).
  • 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".
  • There are two kinds of obstacles, huge blocks, and more elaborate things like houses and trees. Their location and physical shape can be accessed using the "obstacleMap" object (see details in CarAI.cs). Make sure you understand how it works.
  • 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.
  • droneScene.png