Driving MiniGame: Difference between revisions

From Graal Bible
No edit summary
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
Welcome to the Driving MiniGame tutorial.
Welcome to the Driving MiniGame tutorial.


This is a simple and easy car driving example. It is not focused on the actual driving experience itself, but more on introducing new Unity functionalities.
= Intro =


'''Asset Bundles: ''minigame2, env'''''
==== 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.


using the bundle explorer (F10) :
'''Asset Bundles:''' <nowiki>https://www.graalonline.com/playerworlds/downloads/file?name=cars_planes_minigame.unitypackage</nowiki>
 
using the bundle explorer (F10):


'''minigame2:'''
'''minigame2:'''
Line 15: Line 18:
[[File:Minigame2 env.png|frameless]]
[[File:Minigame2 env.png|frameless]]


The script is found in the weapon: '''''3D/Jimmy/Minigame2'''''
Weapon: '''''3D/Samples/Drivingminigame'''''
  findplayer("GraalID").addweapon(this.name);
  findplayer("GraalID").addweapon(this.name);
  this.join("3d_merlin_camera");
  this.join("3d_merlin_camera");
Line 84: Line 87:
  function MyGUI_Minigame_Button1.onAction() {
  function MyGUI_Minigame_Button1.onAction() {
   // Button "Car" has been pressed
   // Button "Car" has been pressed
   Quattro::AssetManagement::LoadAssetBundle("minigame2");
   (@"3D/Dev/AssetManager").loadAssetBundle("minigame2");
   Quattro::AssetManagement::LoadAssetBundle("env");
   (@"3D/Dev/AssetManager").loadAssetBundle("env");
   this.vehicle = "car";
   this.vehicle = "car";
  }
  }
Line 91: Line 94:
  function MyGUI_Minigame_Button2.onAction() {
  function MyGUI_Minigame_Button2.onAction() {
   // Button "Bus" has been pressed
   // Button "Bus" has been pressed
   Quattro::AssetManagement::LoadAssetBundle("minigame2");
   (@"3D/Dev/AssetManager").loadAssetBundle("minigame2");
   Quattro::AssetManagement::LoadAssetBundle("env");
   (@"3D/Dev/AssetManager").loadAssetBundle("env");
   this.vehicle = "bus";
   this.vehicle = "bus";
  }
  }
Line 102: Line 105:
  function onAssetBundleDownloaded(bundlename) {
  function onAssetBundleDownloaded(bundlename) {
   if (bundlename == "env") {
   if (bundlename == "env") {
     this.env = GameObject::createfromassetbundle("env", "assets/jimmyenv2/Environment.prefab");
     this.env = GameObject::fromassetbundle("env", "assets/jimmyenv2/Environment.prefab");
     this.env = Object::Instantiate(Type::GameObject, this.env);
     this.env = Object::Instantiate(Type::GameObject, this.env);
     this.env.transform.position = Vector3::Create(player.x, player.z, player.y);
     this.env.transform.position = v3(player.x, player.z, player.y);
   }
   }
   if (bundlename == "minigame2") {
   if (bundlename == "minigame2") {
     if (this.vehicle == "bus") {
     if (this.vehicle == "bus") {
       dest();
       dest();
       this.vh = GameObject::createfromassetbundle("minigame2", "assets/jimmyminigame2/Veh_Bus_Blue_Z.prefab");
       this.vh = GameObject::fromassetbundle("minigame2", "assets/jimmyminigame2/Veh_Bus_Blue_Z.prefab");
     }
     }
      
      
     if (this.vehicle == "car") {
     if (this.vehicle == "car") {
       dest();
       dest();
       this.vh = GameObject::createfromassetbundle("minigame2", "assets/jimmyminigame2/Veh_Car_Blue_Z.prefab");
       this.vh = GameObject::fromassetbundle("minigame2", "assets/jimmyminigame2/Veh_Car_Blue_Z.prefab");
     }
     }
      
      
     this.vh = Object::Instantiate(Type::GameObject, this.vh);
     this.vh = Object::Instantiate(Type::GameObject, this.vh);
     this.vh.transform.position = Vector3::Create(player.x - 2, player.z, player.y - 5);
     this.vh.transform.position = v3(player.x - 2, player.z, player.y - 5);
     this.vh.AddComponent(Type::RigidBody);
     this.vh.AddComponent(Type::RigidBody);
   
   
     PLAYERMOVEMENT.Freeze();
     PLAYERMOVEMENT.Freeze();
     Quattro3D::PlayerCamera::Instance.FreeCam = true;
     //Quattro3D::PlayerCamera::Instance.FreeCam = true;
    CAMERAMANAGER.freecam = true;
     handleMovement();
     handleMovement();
   }
   }
Line 131: Line 134:
   this.on = true;
   this.on = true;
   SetTimer(0.05);
   SetTimer(0.05);
   this.speed = 40;
   this.speed = 20;
   this.rotation = 400;
   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() {
  function onTimeOut() {
   if(this.on) {
   if(this.on) {
     temp.campos = this.vh.transform.position.Add(Vector3::create(0,10,-7));
     temp.campos = this.vh.transform.position.Add(v3(0,10,-7));
     Quattro3D::PlayerCamera::Instance.transform.position = temp.campos;
     Quattro3D::PlayerCamera::Instance.transform.position = temp.campos;
      
      
     this.vInput = Input::GetAxis("Vertical");
     //this.vInput = Input::GetAxis("Vertical");
      
      
     this.hInput = Input::GetAxis("Horizontal");
     //this.hInput = Input::GetAxis("Horizontal");
     if (this.vInput < 0) 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));
     //this.vh.transform.Translate(Vector3::forward.Mult(Time::deltaTime * this.speed * this.vInput));
     this.vh.transform.Rotate(Vector3::up.Mult(Time::deltaTime * this.rotation * this.hInput));
      
     SetTimer(0.05);
     SetTimer(0.05);
   }
   }
Line 153: Line 164:
  function dest() {
  function dest() {
   this.on = false;
   this.on = false;
   cam1();
   findweapon("3D/Dev/ToolBar/Camera").cam1();
   PLAYERMOVEMENT.unfreeze();
   PLAYERMOVEMENT.unfreeze();
   Object::Destroy(this.vh);
   Object::Destroy(this.vh);
   Object::Destroy(this.env);
   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.
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.


Line 163: Line 175:
The Camera is the point of view. Here we want the Camera to follow the car while driving and not the player.
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:
In '''''function onAssetBundleDownloaded(bundlename)''''' We use:
{| class="wikitable"
{| class="wikitable"
!Quattro3D::PlayerCamera::Instance.FreeCam = true;
!Quattro3D::PlayerCamera::Instance.FreeCam = true;
|}
|}
To free the camera, it won't follow any GameObject.
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.
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 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''''' 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:
{| class="wikitable"
{| class="wikitable"
!temp.campos = this.vh.transform.position.Add(Vector3::create(0,10,-7));
!temp.campos = this.vh.transform.position.Add(v3(0,10,-7));
|}
|}
now:
now:
Line 181: Line 193:
allows us to the get the Camera GameObject and to access its transform component and change its 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 class : '''''3d_merlin_camera'''''
We have implemented default cameras found in Weapon: '''''3D/Dev/ToolBar/Camera'''''


In order to access them, we join the weapon with that class: '''''this.join("3d_merlin_camera");'''''
We access them through the Weapon "'''''3D/Dev/ToolBar/Camera'''''" using:
 
{| class="wikitable"
and we call the function '''''cam1();''''' in '''''function dest()''''' to go back to the default cam.
!''findweapon("3D/Dev/ToolBar/Camera").cam1();''
|}and we call the function '''''cam1();''''' in '''''function dest()''''' to go back to the default cam.


==== <u>New: Unity Input</u> ====
==== <u>New: Unity Input</u> ====

Latest revision as of 09:14, 22 November 2021

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.