Skip to content

Instantly share code, notes, and snippets.

@seferciogluecce
Last active September 20, 2020 09:02
Show Gist options
  • Save seferciogluecce/7e7762589dba0f878b5a192e231723ff to your computer and use it in GitHub Desktop.
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
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