Documente Academic
Documente Profesional
Documente Cultură
Scripts
4.1. Move Cameras
One function of this script is to use the cursor movement to change the position and
rotation of the camera. We want the camera to pan to the right when the mouse is
dragged to the right and tilt up when the mouse is dragged up. To do that, we need
to understand the way unity define mouse position and object rotation.
As we can see from the picture, when we want to pan the camera to the left,
we need to give a negative value to the Y (angle) while the Mouse X variable is
given the negative value. On the other hand, when we want the camera to tilt up,
we need to give a positive value to the X (angle) while the Mouse Y variable is
given negative value. The camera rotation movement script would have this
arrangement involved into it.
As for the camera position movement script, it is sufficient to give the use
values of rotation into the position coordinate. The Z-axis coordinate will, however,
be multiplied by negative so that the camera would face into the game object
(instead of facing away from it).
The script also has the function to execute raycast. Raycast will detect the
clicked object and retrieve the Tag of the object.
1. If the clicked object is tagged White, the tag will change into Red or Blue
based on whose turn it is. This tag is attached to the game blocks and the change of
tag will trigger the strokefinder script (In object array). This will also increment the
turn variable.
2. If the clicked object is tagged Poof or UnPoof, the tag will switch into the
other. This tag is attached to the poof spheres and used to deactivate one layer of
the game objects. This is so that the layers of blocks in the middle are easier to
click.
This script also manages the text that shows the number of turn on the up-
left corner of the game screen. It also manages the text that shows whose turn it is
on the upper middle game screen.
The BtnUI is a Boolean variable that refers to the pause button. BtnUI
=false means that the button has not been clicked. BtnUI = true means that the
button has been clicked and the game is in the pause state. The pause state
disables the camera rotation function and raycast function so that the player cannot
interact with the game during pause time. The mechanic of the pause script will be
explained in section 4.5 Button Manager.
The turn timer is also managed in this script. The turn timer starts when the
player turn switches. The turn timer text is not updated when the game is paused.
At the same time, while the game is paused, the pause timer will start counting.
When the game is unpaused, the turn timer will show the total turn timer (without
pause) subtracted by the total pause time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public Transform target; //define the target object for the camera to orbit
public float distance = 10.0f; //define initial distance from camera toward target
public float xSpeed = 40.0f; //define a variable used to control camera movement speed
public float ySpeed = 40.0f;
public int turn; //define the variable that counts the number of turns
public Text turnText; //define the text that would show the number of turns
public Text middleText; //define the text that would show who's turn it is
public float distanceMin = 7f; //define the minimum distance from camera toward target
public float distanceMax = 15f; //define the maximum distance from camera toward target
private float temps; //a variable used to calculate the difference of time between the click until release
public string hitTag; //the string used to get the tag of raycasted object
hitTag = "Red"; //initialize the first raycast hit as red, as if it was Red's turn before the game
startTime = Time.time;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); //distance is negated so that the camera will face ob
ject
Vector3 position = rotation * negDistance; //calculate the position
if (Input.GetMouseButtonUp(0) && (Time.time - temps) < 0.2){ //if the mouse is released within under 0.2 seco
nd
Debug.Log("Mouse is down");
if (BtnUI == false) {
hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
//^execute raycast to mouse location with the output of hitinfo
}
Debug.Log("Mouse is down");
}
float total = Mathf.Pow (d, 3); //calculate the total number of boxes
if (turn==total) {
middleText.text = "Game Over!"; //update middle text to gameover
middleText.color = Color.green;
}
}
The tag of the object is managed by the MoveCamera script. Specifically, the part
that says: {hitInfo.transform.gameObject.tag = "Blue"} and
{hitInfo.transform.gameObject.tag = "Red"}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public Renderer rend; //define renderer of the block, color is an aspect of rendere
r
void Start()
{
rend = GetComponent<Renderer>(); //get renderer component
}
After all of the blocks are defined into the array. The script will read the
variable hitTag from the MoveCamera script. When the hitTag variable changes,
the strokefinder function would be triggered to find and calculate the number of the
stroke that the hitTag color has achieved.
A stroke is when a line of blocks have the same color. In the picture above,
there is 1 blue straight stroke and 1 red diagonal stroke.
The strokefinder is divided into 4 parts; the first part would find all the
straight strokes that face the x-axis along with the diagonal strokes that faces the
same direction. The next 2 parts would be for the y-axis and z-axis. The last part
would find the strokes across the spatial diagonal of the cube.
The strokes from all the parts would be counted and will be displayed into the
score texts under the turn text.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
int colorCount; //counts the number of straight line of boxes with the same tag a
s color
int diagonalCount; //counts the number of a diagonal line of boxes
int diagonal2Count; //counts the number of the other diagonal line of boxes
int[] SpaDi = new int[4] ; //count the number for each space diagonal line of boxe
s
public int strokeCount; //count the number of total strokes made by the same pla
yer
public Text BlueText; //the text that shows how many strokes had been made by
blue player
public Text RedText; //the text that shows how many strokes had been made by
red player
strokeCount = 0;
//get the variable hitTag from MoveCamera script and copy it into the variable
color
GameObject kamera = GameObject.Find ("Main Camera");
MoveCamera movecamera = kamera.GetComponent<MoveCamera>();
color = movecamera.hitTag;
//this part will find the straight strokes along the x axis
//it will also find the diagonal strokes on each of the "a" layer
for (a = 0; a < d; a++) {
for (b = 0; b < d; b++) {
for (c = 0; c < d; c++) {
if (block [a] [b] [c].CompareTag (color)) colorCount++;
if (b==c && block [a] [b] [c].CompareTag (color)) diagonalCount++;
v = d - c - 1;
if (b==v && block [a] [b] [c].CompareTag (color)) diagonal2Count++;
}
if (colorCount == d) strokeCount++;
colorCount = 0;
}
if (diagonalCount == d) strokeCount++;
if (diagonal2Count == d) strokeCount++;
diagonalCount = 0;
diagonal2Count = 0;
}
colorCount = 0;
diagonalCount = 0;
diagonal2Count = 0;
//y axis
for (b = 0; b < d; b++) {
for (c = 0; c < d; c++) {
for (a = 0; a < d; a++) {
if (block [a] [b] [c].CompareTag (color)) colorCount++;
if (c==a && block [a] [b] [c].CompareTag (color)) diagonalCount++;
v = d - a -1;
if (c==v && block [a] [b] [c].CompareTag (color)) diagonal2Count++;
}
if (colorCount == d) strokeCount++;
colorCount = 0;
}
if (diagonalCount == d) strokeCount++;
if (diagonal2Count == d) strokeCount++;
diagonalCount = 0;
diagonal2Count = 0;
}
colorCount = 0;
diagonalCount = 0;
diagonal2Count = 0;
//z axis
for (c = 0; c < d; c++) {
for (a = 0; a < d; a++) {
for (b = 0; b < d; b++) {
if (block [a] [b] [c].CompareTag (color)) colorCount++;
if (a==b && block [a] [b] [c].CompareTag (color)) diagonalCount++;
v = d - b - 1;
if (a==v && block [a] [b] [c].CompareTag (color)) diagonal2Count++;
}
if (colorCount == d) strokeCount++;
colorCount = 0;
}
if (diagonalCount == d) strokeCount++;
if (diagonal2Count == d) strokeCount++;
diagonalCount = 0;
diagonal2Count = 0;
}
//last 4 space diagonal
for (a=0 ;a<d ;a++){
if (block [a] [a] [a].CompareTag(color)) SpaDi[0]++;
if (block [a] [d - a - 1] [a].CompareTag(color)) SpaDi[1]++;
if (block [d - a - 1] [a] [a].CompareTag(color)) SpaDi[2]++;
if (block [d - a - 1] [d - a - 1] [a].CompareTag(color)) SpaDi[3]++;
}
colorCount = 0;
diagonalCount = 0;
diagonal2Count = 0;
The first part of the script is to define an array of the spheres and block
layers. The plane is defined by as the child of the Parent Gameobject of all blocks.
The poof spheres are defined as the child of poof game object (where the script is
attached to).
The sphere would be paired with the layer of blocks when they have the same array
number. For example, poof[2] (the name of the sphere) is paired with plane[2] (the
name of the layer).
When the tag of poof[2] is UnPoof, the layer[2] would appear (active).
When the tag of poof[2] is Poof, the plane [2] would disappear (deactivate).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
int a;
int d=4;
if (poof[a].CompareTag("UnPoof")){
plane [a].SetActive (true);
}
}
}
2. The Timer with pause: The timer that does not count when the game is paused.
When the game is paused, the absolute timer is not affected. But the text that
would show Timer with pause does not update. At the same time, another timer
called pause time (not shown in the game) will start counting. When the game
resumes, Timer with pause is calculated by Absolute timer subtracted by Pause
time.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
void start(){
startTime = Time.time;
}
void Update(){
float ft = Time.time - startTime;
int t = (int) ft;
if (BtnUI == false) {
ft = ft - pauseTime;
t = (int) ft;
timerWithPause.text = "Timer With Pause = " + hours + ":" + minutes + ":" + seconds;
}
}
}