Driving MiniGame

From Graal Bible

Welcome to the Driving MiniGame tutorial.

Intro

DrivingMiniGame

This is a simple and easy car driving example. In the assetbundle, there's an environment that we load and instantiate and multiple cars that we can spawn. This game is not focused on the actual driving experience itself, but more on introducing new Unity functionalities.

Asset Bundles: https://www.graalonline.com/playerworlds/downloads/file?name=cars_planes_minigame.unitypackage

using the bundle explorer (F10):

minigame2:

Minigame2.png

env:

Minigame2 env.png

Weapon: 3D/Samples/Drivingminigame

findplayer("GraalID").addweapon(this.name);
this.join("3d_merlin_camera");

//#CLIENTSIDE
function onPlayerChats() {
  if (player.chat == "dest") dest();
  if (player.chat == "drive") drive();
}

function drive() {
  destLevelName = "only_ground.nw";
  x = 19;
  y = 10;
  z = 0.7;//0.7;
  WARPMANAGER.Warp(x, y ,z ,destLevelName);

  sleep(1);
 
  new GuiWindowCtrl("MyGUI_Minigame_Window1") {
    profile = GuiBlueWindowProfile;
    style = $pref::Video::defaultguistyle;
    clientrelative = true;
    clientextent = "320,240";

    canmove = true;
    canresize = true;
    closequery = false;
    destroyonhide = false;
    text = "Window 1";
    x = 490;
    y = 242;

    new GuiButtonCtrl("MyGUI_Minigame_Button1") {
      profile = GuiBlueButtonProfile;
      text = "Car";
      width = 80;
      x = 77;
      y = 31;
    }
    new GuiButtonCtrl("MyGUI_Minigame_Button2") {
      profile = GuiBlueButtonProfile;
      height = 31;
      text = "Bus";
      width = 80;
      x = 77;
      y = 69;
    }
    new GuiButtonCtrl("MyGUI_Minigame_Button3") {
      profile = GuiBlueButtonProfile;
      height = 31;
      text = "Quit";
      width = 80;
      x = 78;
      y = 109;
    }
    new GuiTextCtrl("MyGUI_Minigame_Text1") {
      profile = GuiBlueTextProfile;
      height = 20;
      text = "Select Type:";
      width = 71;
      x = 11;
      y = 5;
    }
  }
}

function MyGUI_Minigame_Button1.onAction() {
  // Button "Car" has been pressed
  (@"3D/Dev/AssetManager").loadAssetBundle("minigame2");
  (@"3D/Dev/AssetManager").loadAssetBundle("env");
  this.vehicle = "car";
}

function MyGUI_Minigame_Button2.onAction() {
  // Button "Bus" has been pressed
  (@"3D/Dev/AssetManager").loadAssetBundle("minigame2");
  (@"3D/Dev/AssetManager").loadAssetBundle("env");
  this.vehicle = "bus";
}

function MyGUI_Minigame_Button3.onAction() {
  dest();
}

function onAssetBundleDownloaded(bundlename) {
  if (bundlename == "env") {
    this.env = GameObject::fromassetbundle("env", "assets/jimmyenv2/Environment.prefab");
    this.env = Object::Instantiate(Type::GameObject, this.env);
    this.env.transform.position = v3(player.x, player.z, player.y);
  }
  if (bundlename == "minigame2") {
    if (this.vehicle == "bus") {
      dest();
      this.vh = GameObject::fromassetbundle("minigame2", "assets/jimmyminigame2/Veh_Bus_Blue_Z.prefab");
    }
    
    if (this.vehicle == "car") {
      dest();
      this.vh = GameObject::fromassetbundle("minigame2", "assets/jimmyminigame2/Veh_Car_Blue_Z.prefab");
    }
    
    this.vh = Object::Instantiate(Type::GameObject, this.vh);
    this.vh.transform.position = v3(player.x - 2, player.z, player.y - 5);
    this.vh.AddComponent(Type::RigidBody);

    PLAYERMOVEMENT.Freeze();
    //Quattro3D::PlayerCamera::Instance.FreeCam = true;
    CAMERAMANAGER.freecam = true;
    handleMovement();
  }
}

function handleMovement() {
  this.on = true;
  SetTimer(0.05);
  this.speed = 20;
  this.rotation = 400;
}

function onFixedUpdate() {
  this.vInput = Input::GetAxis("Vertical");
  this.vh.transform.Translate(Vector3::forward.Mult(Time::deltaTime * this.speed * this.vInput));
    
  this.hInput = Input::GetAxis("Horizontal");
  this.vh.transform.Rotate(Vector3::up.Mult(Time::deltaTime * this.rotation * this.hInput * this.vInput));
}

function onTimeOut() {
  if(this.on) {
    temp.campos = this.vh.transform.position.Add(v3(0,10,-7));
    Quattro3D::PlayerCamera::Instance.transform.position = temp.campos;
    
    //this.vInput = Input::GetAxis("Vertical");
    
    //this.hInput = Input::GetAxis("Horizontal");
    //if (this.vInput < 0) this.hInput = - Input::GetAxis("Horizontal");

    //this.vh.transform.Translate(Vector3::forward.Mult(Time::deltaTime * this.speed * this.vInput));
    
    SetTimer(0.05);
  }
}

function dest() {
  this.on = false;
  findweapon("3D/Dev/ToolBar/Camera").cam1();
  PLAYERMOVEMENT.unfreeze();
  Object::Destroy(this.vh);
  Object::Destroy(this.env);
}

Here we create a UI for the player to choose what vehicle to choose, and we load and instantiate the prefab based on the choice.

New: Camera

The Camera is the point of view. Here we want the Camera to follow the car while driving and not the player.

In function onAssetBundleDownloaded(bundlename) We use:

Quattro3D::PlayerCamera::Instance.FreeCam = true;

To free the camera. This will free it from following the player GameObject.

Then in function onTimeOut() we want the camera position to be updated with the vehicle's position.

temp.campos is the variable we store the camera's position in. We want it to be behind and above the vehicle for a good view. This is why we get the vehicle's position and add a Vector3 to it:

temp.campos = this.vh.transform.position.Add(v3(0,10,-7));

now:

Quattro3D::PlayerCamera::Instance.transform.position

allows us to the get the Camera GameObject and to access its transform component and change its position.

We have implemented default cameras found in Weapon: 3D/Dev/ToolBar/Camera

We access them through the Weapon "3D/Dev/ToolBar/Camera" using:

findweapon("3D/Dev/ToolBar/Camera").cam1();

and we call the function cam1(); in function dest() to go back to the default cam.

New: Unity Input

Input::GetAxis("Vertical");

"Vertical" / "Horizontal"

It checks the arrows pressed and gives a value in the range of (-1 to 1) -1 indicating "down", 1 "up" and 0 "neutral".

We use it here to translate and rotate the vehicle.