# Determine which face of a cube is facing the camera

I have a Cube with a unique color for each face. I made a script where I can swipe and rotate the Cube in increments of 90 degrees in each direction. what’s the best way to determine which face of a the Cube is facing the camera.

Does your camera move, and does the cube only rotate on one axis? If yes can figure it out just by looking at the cubes rotation.

Otherwise if you could use a raycast from the camera and its hit info to get the triangle I’d or the UV coord of the hit, and keep a list of what color each triangle is to reference back.

Yes the camera is fixed, but the cube will rotate on different axis.
I tried to compare the current rotation of the cube with the possible rotation but I can’t figure out how many are they.
also do you have some examples on how to use raycasting (this is my first project in unity)

well here is a possible way of many of doing this.

make sure your cube is using a mesh collider instead of a box collider, since this works by reading the triangleID from the collider than looking up a color by id in a list.

``````using UnityEngine;
using System.Collections;

public class CubeSide : MonoBehaviour
{
private Color[] colors =
{
Color.red, Color.red, Color.green, Color.green, Color.blue, Color.blue,
Color.yellow, Color.yellow, Color.black, Color.black, Color.white, Color.white,
};

private void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Debug.Log(colors[hit.triangleIndex]);
}
}
}
}
``````

the reason why i got entries for each of the 6 colors is because this reads the triangleID, and it takes 2 triangles to make a cubes face.

a other possible way of doing this could be to have 6 gameObjects with there own colliders parted under the cube and alinged to each face, and just find the color based on which one got the rayhit.

I would calculate dotX = Vector3.Dot( camDir, cubeXaxis ), same for dotY and dotZ. Then select extreme ( now you will know if its right/let, top/bottom or front/back) and based on its sign if its right or left, …

this is what I ended up doing:

``````using System.Collections.Generic;
.
.
.
Dictionary<Vector3, Face> Faces = new Dictionary<Vector3, Face>()
{
{new Vector3(0.0f, 0.0f, 1.0f), Face.Yellow},
{new Vector3(0.0f, -1.0f, 0.0f), Face.Blue},
{new Vector3(0.0f, 0.0f, -1.0f), Face.Pink},
{new Vector3(0.0f, 1.0f, 0.0f), Face.Purple},
{new Vector3(-1.0f, 0.0f, 0.0f), Face.Green},
{new Vector3(1.0f, 0.0f, 0.0f), Face.Red},
};
enum Face { Yellow, Blue, Pink, Purple, Green, Red };
.
.
.
void updateFace () {
var x = Mathf.Round(Vector3.Dot(Camera.main.transform.forward, transform.right));
var y = Mathf.Round(Vector3.Dot(Camera.main.transform.forward, transform.up));
var z = Mathf.Round(Vector3.Dot(Camera.main.transform.forward, transform.forward));

Debug.Log(Faces[new Vector3(x, y, z)]);
}
``````

The easiest way would be to Raycast from each face this.transform.forward, this.transform.right, this.transform.up and the negation of those will get you all the faces. Based on the RaycastHitInfo object take the crossproduct, if you get a zero vector then the face is facing the camera. Or what you can do is take the angle between the two vectors. The closer to 0 the more the two vectors are pointing in the same direction, you can create a threshold and cross check that.

So this will log the face of the cube that is currently in view of the camera. Your code is hardcoded for the Up, Down, Right, Left of a cube that is static on X, and Z axis. The code below does not matter of the orientation of the camera it will always give you the correct face even if it twists or rotates.

``````using UnityEngine;
using System.Collections;

public class CubeFace : MonoBehaviour {
[SerializeField] float threshHold = 5.0f;
[SerializeField] float rayLength = 1.0f;

void LateUpdate() {
UnityEngine.Debug.DrawRay(this.transform.position, this.transform.up * this.rayLength, Color.red);

UnityEngine.Debug.DrawRay(this.transform.position, -this.transform.up * this.rayLength, Color.magenta);

UnityEngine.Debug.DrawRay(this.transform.position, this.transform.forward * this.rayLength, Color.blue);

UnityEngine.Debug.DrawRay(this.transform.position, -this.transform.forward * this.rayLength, Color.cyan);

UnityEngine.Debug.DrawRay(this.transform.position, this.transform.right * this.rayLength, Color.yellow);

UnityEngine.Debug.DrawRay(this.transform.position, -this.transform.right * this.rayLength, Color.gray);

// theta = arcos( a • b / |a| • |b|)
float upAngle = Mathf.Acos(Vector3.Dot(this.transform.up, Camera.main.transform.forward) / (this.transform.up.magnitude * Camera.main.transform.forward.magnitude));
upAngle *= 180.0f / Mathf.PI; // In Degrees not radians.

float downAngle = Mathf.Acos(Vector3.Dot(-this.transform.up, Camera.main.transform.forward) / (this.transform.up.magnitude * Camera.main.transform.forward.magnitude));
downAngle *= 180.0f / Mathf.PI; // In Degrees not radians.

float forwardAngle = Mathf.Acos(Vector3.Dot(this.transform.forward, Camera.main.transform.forward) / (this.transform.forward.magnitude * Camera.main.transform.forward.magnitude));
forwardAngle *= 180.0f / Mathf.PI; // In Degrees not radians.

float backwardAngle = Mathf.Acos(Vector3.Dot(-this.transform.forward, Camera.main.transform.forward) / (this.transform.forward.magnitude * Camera.main.transform.forward.magnitude));
backwardAngle *= 180.0f / Mathf.PI; // In Degrees not radians.

float rightAngle = Mathf.Acos(Vector3.Dot(this.transform.right, Camera.main.transform.forward) / (this.transform.right.magnitude * Camera.main.transform.forward.magnitude));
rightAngle *= 180.0f / Mathf.PI; // In Degrees not radians.

float leftAngle = Mathf.Acos(Vector3.Dot(-this.transform.right, Camera.main.transform.forward) / (this.transform.right.magnitude * Camera.main.transform.forward.magnitude));
leftAngle *= 180.0f / Mathf.PI; // In Degrees not radians.

if(upAngle < this.threshHold) {
UnityEngine.Debug.Log("Top Face is facing the Camera");
}

if(downAngle < this.threshHold) {
UnityEngine.Debug.Log("Bottom Face is facing the Camera");
}

if(forwardAngle < this.threshHold) {
UnityEngine.Debug.Log("Forward Face is facing the Camera");
}

if(backwardAngle < this.threshHold) {
UnityEngine.Debug.Log("Backward Face is facing the Camera");
}

if(rightAngle < this.threshHold) {
UnityEngine.Debug.Log("Right Face is facing the Camera");
}

if(leftAngle < this.threshHold) {
UnityEngine.Debug.Log("Left Face is facing the Camera");
}
}
}
``````
1 Like