Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- namespace YounGenTech {
- public class Dice : MonoBehaviour {
- public Vector3[] side = new Vector3[] { Vector3.forward, Vector3.down, Vector3.left, Vector3.right, Vector3.up, Vector3.back };
- public int currentTopSide = -1;
- void Update() {
- currentTopSide = GetTopSide();
- }
- public int GetTopSide() {
- return GetNearestToDirection(Vector3.up);
- }
- public int GetNearestToDirection(Vector3 direction) {
- int index = -1;
- float highestDot = 0;
- for(int i = 0; i < side.Length; i++) {
- float dot = Vector3.Dot(direction, transform.TransformDirection(side[i]));
- if(dot > highestDot) {
- index = i;
- highestDot = dot;
- }
- }
- return index;
- }
- void OnDrawGizmos() {
- if(currentTopSide > -1 && currentTopSide < side.Length) {
- Gizmos.color = Color.yellow;
- Gizmos.DrawRay(transform.position, transform.TransformDirection(side[currentTopSide]));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment