Hello everyone. I am making a project with cubes which rotate on a grid. I paint each cube with three colors - on a face and its opposite. A cube can touch another one when faces have the same colors. In other cases, it comes back to its previous place - and movement isn't possible. I am trying to find a way to compare cubes' eulerAngles checking if they are the same. However it is not enough because of a particularity - each color has been painted on two faces for each cube. Do you have an idea how I could check this state? I have to say that in my project, I don't use any collider or rigidbody - only physics. I could use colliders with tags to check if faces have the same color, but I want to avoid any collider. Thank you for your help. Take a look at these pictures so as to understand the main idea - main gameplay. https://img.itch.zone/aW1hZ2UvNjQ2MDE5LzQ1NjIyMDcucG5n/original/+aPbl/.png https://img.itch.zone/aW1hZ2UvNjQ2MDE5LzQ1NjIyMDgucG5n/original/fBOdgI.png
If I understood correctly, the colors correspond to the axes of the cube (Vector3s transform.right/.up/.forward) I assume you already have a way to find neighboring cubes, so lets say you have cubes A and B Code (CSharp): d = (A.transform.position - B.transform.position).normalized; var isX_A = Mathf.Abs( Vector3.Dot( d, A.transform.right ) ) > 0.9f; var isY_A = Mathf.Abs( Vector3.Dot( d, A.transform.up ) ) > 0.9f; var isZ_A = Mathf.Abs( Vector3.Dot( d, A.transform.forward ) ) > 0.9f var isX_B = Mathf.Abs( Vector3.Dot( d, B.transform.right ) ) > 0.9f; var isY_B = Mathf.Abs( Vector3.Dot( d, B.transform.up ) ) > 0.9f; var isZ_B = Mathf.Abs( Vector3.Dot( d, B.transform.forward ) ) > 0.9f; var sameColorTouching = isX_A == isX_B && isY_A == isY_B && isZ_A == isZ_B; This uses the fact, that the absolute of the dot product of two vectors is large if they point in the same or opposite direction and is close to zero when they are orthogonal to each other.
Hello. Thank you for your help. I really appreciate. Yes! You understaood correctly - the colors correspond to the axis of the cubes. I did this code, but I guess that it could be improved. Code (CSharp): // simple function to compare two values - return the higher private int returnHigher(int first, int second) { if (first >= second) return first - second; else return second - first; } (...) foreach (GameObject g in cc) { // excludes cube player which are not in our radius if (g != thisOne && Vector3.Distance(g.transform.position, thisOne.transform.position) < 1.33f) { // absolute them int x = Mathf.Abs((int)thisOne.transform.eulerAngles.x); int y = Mathf.Abs((int)thisOne.transform.eulerAngles.y); int z = Mathf.Abs((int)thisOne.transform.eulerAngles.z); int xx = Mathf.Abs((int)g.transform.eulerAngles.x); int yy = Mathf.Abs((int)g.transform.eulerAngles.y); int zz = Mathf.Abs((int)g.transform.eulerAngles.z); // in order to save values int xxx = returnHigher(x, xx); int yyy = returnHigher(y, yy); int zzz = returnHigher(z, zz); if ((xxx == 0 || xxx == 180) && (yyy == 0 || yyy == 180) && (zzz == 0 || zzz == 180)) Debug.Log("it works as expected"); else computeMove(-direction, thisOne, true, true); } } Now I want to test yours which could be better
You can just use Mathf, it has some useful functions Code (CSharp): Mathf.Max( first, second ); And compare using normalized and magnitude Code (CSharp): //returns 0~2 range, depending on rotation difference //will return 2 if they are facing each other (180 deg) //will return 1 if they are on 90 degrees //will return 0 if they are facing the same direction private float ColorCheck(Transform playerCube, Transform targetCube) { return ( playerCube.transform.eulerAngles.normalized - targetCube.transform.eulerAngles.normalized ).magnitude; }
Thank you everyone for your kind help. As we can read somewhere "without counsel purposes are disappointed: but in the multitude of counselors they are established". I really appreciate all your help. I did many tests, but finally the best solution has been given by tjmaul. This function is very stable, more than mine. @N1ghtSiren - your explanation is not bad, but it doesn't fit with some features in my project. However, thank you for your suggestion. I wish you the best, my friends. Thank you ++