Skip to content

Tutorial ‐ Step 2

ptaillandier edited this page Feb 1, 2024 · 23 revisions

Creation of the Unity basic game

Creation and setting of the project

To start the creation of the Unity game, a first step consists in cloning or just downloading the Simple template Unity and add it in the Unity Hub. You can then open it.

Download

This project includes 3 important scenes (Assets/Scenes folder): The "Start Up" menu, which is the scene launched at the start of the game; the "IP Menu", which lets you configure the IP of the computer running GAMA or the middleware; and the "Main Scene" in the Demo folder. In this tutorial, we will only modify the latter scene.

TutorialScene

Player agent settings

By default, the “Main Scene” includes an FPS player. This type of player is subject to gravity and moves at ground level, considering geometries with a collider as obstacles. In our case, we prefer to have a “decision maker” view from the sky. We will remove the FPS player (right-click it and then delete). In Prefabs, GAMA Link, Player, drag and drop the "GodViewPlayer" into the scene.

TutorialPlayer

Game manager settings

In the main scene, inspect the “Game Manager” in “Managers”. In the Simulation Manager, drag and drop the GodViewPlayer game object from the Main Scene to "Player". Then, as we only have one type of agent that we send to Unity ("people" agents), we will only keep one element in the "Agents", "Rotations", "Rotations coefficient", "Y Values" and "Size Factor" lists. These lists respectively represent the prefab used to represent the agents to send (in our case, the "people" agents), the rotation to apply to this prefab (according to the Y axis), the rotation coefficient to apply to it (according to the Y axis), the offset to apply along the Y axis and the size scale of the prefab. In this tutorial, we will define them respectively as for the list of agents, the "CAR" prefab (found in Assets/Prefabs/Visual Prefabs/City/Vehicles/), for Rotations, 90, for the Rotation Coefficient, - 1, for the Y value, -1, and 2.0, for the size factor. The manager allows as well to set different conversion coefficient between Unity and GAMA. In this tutorial, we want to use the same coordinate in GAMA and Unity, we thus set GAMA CRS Coeff X, Y to 1.0; GAMA CRS Offset X,Y,Z to 0.0.

TutorialGameManager

An important point to note concerns the tags used in the model, particularly those sent with the geometries: these must have already been defined in the Unity project. To define a new tag, just inspect any game object in the scene, then in "Tag", select "Add Tag". Then click on the "+" at the end of the tag list and define the new tag.

TutorialAddTag1 TutorialAddTag2

Debug Overlay

By default, the main scene includes a game object called "Debug Overlay", which displays all messages written in the C# script (with the command Debug.Log("my message") ;). In this tutorial, we won't be using it, so you can either remove the "Debug Overlay" game object from the scene, or disable it by unchecking it in the inspector (when you select "Debug Overlay" in the scene, uncheck the box to the left of "Debug Overlay" in the inspector). If you still wish to use it, you must define for the game object "Debug Overlay" (in the Inspector), the rendering camera: drag the "GodViewPlayer/XR Origin (XR Rig)/Camera Offset/Main Camera" component into this attribute.

TutorialDebugOverlay

GodViewPlayer

In this tutorial, we'll be using the GodViewPlayer game object to represent the player, which allows the player to move horizontally with the right joystick and vertically with the left. The first thing we'll do is define the player's initial vertical location. To do this, inspect the GodViewPlayer game object and change the position Y value to 200. We'll also change the movement speed to make it faster. In "GodViewPlayer/XR Origin (XR Rig)", set the "Move Vertical" script speed to 10 and the "Move Horizontal" script speed to 20 and the rotation speed to 10.

TutorialGodViewPlayer

Test of the model

The model can be launched in different ways: from a headset/directly from unity in play mode - with/without middleware. The middleware makes it possible to manage multiple connections and in particular multi-player game. However, its use requires launching third-party software (middleware). As the objective of this tutorial is to launch a game with a single player, we will just use the direct connection to GAMA.

We will see in the following two ways to launch the game: from Unity in play mode; from a VR headset. In all cases it will be necessary to launch the simulation from GAMA before, i.e. start the "vr_xp" experiment of model "traffic_model-VR.gaml".

Without the headset (debug mode) and Without the middleware

Open the script file WebSocketConnector in Assets/Scripts/Gama Provider/Connection. Set DesktopMode to true, and UseMiddleWare to false.

 protected bool DesktopMode = true;
 protected bool UseMiddlewareDM = false; //only for Desktop mode

TutorialDesktopMode

You can now push on the "Play" button of Unity.

With the headset and Without the middleware

Open the script file WebSocketConnector in Assets/Scripts/Gama Provider/Connection. Set DesktopMode to false, and UseMiddleWare to false.

 protected bool DesktopMode = false;
 protected bool UseMiddlewareDM = false; //only for Desktop mode

Open the 3 Scenes (Startup Menu, IP Menu and Main Scene) and connect the headset to the computer (and authorize the connection). In the File Menu, choose "Build Setting". In this panel, select "Android" then click on "Switch platform". In the "Scene in Build" panel, be sure to have the three Scene (first, Startup Scene, then IP Menu and at last, Main Scene). Then, click on Build and Run.

Build-setting

Clone this wiki locally