Driving MiniGame: Difference between revisions

From Graal Bible
No edit summary
No edit summary
Line 4: Line 4:


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


using the bundle explorer (F10) :
using the bundle explorer (F10):


'''minigame2:'''
'''minigame2:'''
Line 18: 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("Graal5918039").addweapon(this.name);
this.join("3d_merlin_camera");
   
   
  //#CLIENTSIDE
  //#CLIENTSIDE
Line 134: Line 133:
   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));
  }
  }
   
   
Line 143: Line 150:
     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 156: Line 163:
  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 166: Line 174:
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(Vector3::create(0,10,-7));
Line 184: Line 192:
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> ====

Revision as of 13:11, 11 July 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: minigame2, env

using the bundle explorer (F10):

minigame2:

Minigame2.png

env:

Minigame2 env.png

Weapon: 3D/Samples/Drivingminigame

findplayer("Graal5918039").addweapon(this.name);

//#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
  Quattro::AssetManagement::LoadAssetBundle("minigame2");
  Quattro::AssetManagement::LoadAssetBundle("env");
  this.vehicle = "car";
}

function MyGUI_Minigame_Button2.onAction() {
  // Button "Bus" has been pressed
  Quattro::AssetManagement::LoadAssetBundle("minigame2");
  Quattro::AssetManagement::LoadAssetBundle("env");
  this.vehicle = "bus";
}

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

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

    PLAYERMOVEMENT.Freeze();
    Quattro3D::PlayerCamera::Instance.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(Vector3::create(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(Vector3::create(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.