GibTreaty

Dice

Jan 2nd, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. namespace YounGenTech {
  4.     public class Dice : MonoBehaviour {
  5.  
  6.         public Vector3[] side = new Vector3[] { Vector3.forward, Vector3.down, Vector3.left, Vector3.right, Vector3.up, Vector3.back };
  7.  
  8.         public int currentTopSide = -1;
  9.  
  10.         void Update() {
  11.             currentTopSide = GetTopSide();
  12.         }
  13.  
  14.         public int GetTopSide() {
  15.             return GetNearestToDirection(Vector3.up);
  16.         }
  17.  
  18.         public int GetNearestToDirection(Vector3 direction) {
  19.             int index = -1;
  20.             float highestDot = 0;
  21.  
  22.             for(int i = 0; i < side.Length; i++) {
  23.                 float dot = Vector3.Dot(direction, transform.TransformDirection(side[i]));
  24.  
  25.                 if(dot > highestDot) {
  26.                     index = i;
  27.                     highestDot = dot;
  28.                 }
  29.             }
  30.  
  31.             return index;
  32.         }
  33.  
  34.         void OnDrawGizmos() {
  35.             if(currentTopSide > -1 && currentTopSide < side.Length) {
  36.                 Gizmos.color = Color.yellow;
  37.                 Gizmos.DrawRay(transform.position, transform.TransformDirection(side[currentTopSide]));
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment