Last active
September 20, 2020 09:02
-
-
Save seferciogluecce/7e7762589dba0f878b5a192e231723ff to your computer and use it in GitHub Desktop.
Find Upper Side Of Dice In Unity Using Vector Operations, tutorial at https://youtu.be/HEUdIavIvlQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class GuessUpperSide : MonoBehaviour | |
{ | |
public Text UpperSideTxt; | |
public Vector3Int DirectionValues; | |
private Vector3Int OpposingDirectionValues; | |
readonly List<string> FaceRepresent = new List<string>() {"", "1", "2", "3", "4", "5", "6"}; | |
//readonly List<string> FaceRepresent = new List<string>() {"","One","Two","Three","Four","Five","Six"}; | |
//readonly List<string> FaceRepresent = new List<string>() {"","I","II","III","IV","V","VI"}; | |
void Start() | |
{ | |
OpposingDirectionValues = 7 * Vector3Int.one - DirectionValues; | |
} | |
void Update() | |
{ | |
if (transform.hasChanged) | |
{ | |
if ( Vector3.Cross(Vector3.up, transform.right).magnitude < 0.5f) //x axis a.b.sin theta <45 | |
//if ((int) Vector3.Cross(Vector3.up, transform.right).magnitude == 0) //Previously | |
{ | |
if (Vector3.Dot(Vector3.up, transform.right) > 0) | |
{ | |
UpperSideTxt.text = FaceRepresent[DirectionValues.x]; | |
} | |
else | |
{ | |
UpperSideTxt.text = FaceRepresent[OpposingDirectionValues.x]; | |
} | |
} | |
else if ( Vector3.Cross(Vector3.up, transform.up).magnitude <0.5f) //y axis | |
{ | |
if (Vector3.Dot(Vector3.up, transform.up) > 0) | |
{ | |
UpperSideTxt.text = FaceRepresent[DirectionValues.y]; | |
} | |
else | |
{ | |
UpperSideTxt.text = FaceRepresent[OpposingDirectionValues.y]; | |
} | |
} | |
else if ( Vector3.Cross(Vector3.up, transform.forward).magnitude <0.5f) //z axis | |
{ | |
if (Vector3.Dot(Vector3.up, transform.forward) > 0) | |
{ | |
UpperSideTxt.text = FaceRepresent[DirectionValues.z]; | |
} | |
else | |
{ | |
UpperSideTxt.text = FaceRepresent[OpposingDirectionValues.z]; | |
} | |
} | |
transform.hasChanged = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment