Identifying multiple transform values to trigger different conditions.

So I have a cube, lets call it a dice (6 unique sides each with a different condition). The dice is viewed in isometric so 3 sides can be viewed at all times. this falls and can be rotated as it falls, think of it as a 3D city building tetris game. so with 3 sides view-able, there are 24 different ways it could land, and every single way needs to trigger a different set of actions (material change, which surfaces work for path-finding and adding/detracting scores).
So the rotation needs to be analysed at the moment it collides with the map. I have some script that rounds its rotation to the nearest 90 degrees, but the identifier statement I haven’t managed to figure out. I have tried the following:

private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject)
        {
        var: Vector3 vec = transform.eulerAngles;
            vec.x = Mathf.Round(vec.x / 90) * 90;
            vec.y = Mathf.Round(vec.y / 90) * 90;
            vec.z = Mathf.Round(vec.z / 90) * 90;
            transform.eulerAngles = vec;
            Debug.Log(transform.eulerAngles);
       
            rigitbody.useGravity = false;
            rigitbody.constraints = RigidbodyConstraints.FreezeAll;
           
            if (transform.eulerAngles.x == 0 && transform.eulerAngles.y == 0 && transform.eulerAngles.z == 0)
            {
                Debug.Log("working");
                rend.sharedMaterial = material[0];
                transform.rotation = Quaternion.Slerp(transform.rotation, originalRotationValue, Time.time * smooth);
            }

        }

    }

and

if (transform.eulerAngles. == new Vector3(0,0,0)
{
Debug.Log("working");
}

and tried a few different combinations with collision in the if statements etc. Currently it reads with no errors but doesn’t actually actuate the scripts beneath it (Haven’t yet had the ā€œworkingā€ statement pop up). So my first problem is this ā€˜if statement’ line doesn’t recognize it. if I go back into unity the game object has the exact values that it should be reading.

My second issue is that my plan to address the 24 different conditions was to repeat this line 24 times with different parameters beneath each one (lots of else ifs) - I’m very new to coding and haven’t had much support in my learning but I’m sure there’s probably a much more efficient way to go about this. any ideas are appreciated. Sorry if this is an obvious fix but I’m under a tight deadline and everything I try doesn’t seam to work, and this game won’t function without this bit working!
Cheers

I don’t know what it means in C# to put ā€œvar:ā€ in front of a declaration, and in your second code snippet line 1 has a weirdly-placed period and is missing a closing parenthesis. So there may be things going on in your code that I haven’t sussed out.

But here’s one likely problem: You should basically never check whether two floats are equal, because rounding errors mean that they almost never will be. Try Mathf.Approximately() instead, or use > and < to check if they’re within some range.

You should also be careful about what range of numbers you’re getting for angles; sometimes computer functions use angles in the range of 0 to 360, sometimes in the range of -180 to +180, and sometimes they aren’t guaranteed to be clamped at all. I don’t remember offhand what Unity does for transform.eulerAngles.

I’m also not sure that rounding all the Euler angles to a multiple of 90 is actually going to be an accurate way of determining the die’s facing. Notice that that gives you 444 = 64 combinations, not the 24 you said you were expecting. Sometimes two different sets of numbers in the Euler angles actually give the same final rotation, and the same number in one spot sometimes has a different practical effect depending on the other numbers.

For your second issue, you can probably work out a way to do all of your tests using loops. If we ignore my previous worry and assume you just want to check all 3 axes for multiples of 90 degrees, you could do something like:

for (int i = 0; i < 4; ++i)
{
    if (Mathf.Approximately(transform.eulerAngles.x, 90*i))
    {
        for (int j = 0; j < 4; ++j)
        {
            if (Mathf.Approximately(transform.eulerAngles.y, 90*j))
            {
                for (int k = 0; k < 4; ++k)
                {
                    if (Mathf.Approximately(transform.eulerAngles.z, 90*k))
                    {
                        // Found a match
                        int resultNum = i + 4*j + 16*k; // A number from 0 to 63 indicating the combination of rotations
                    }
                }
            }
        }
    }
}

However, if you need to trigger different behavior for every possible result you might still need to write out all the options one at a time (possibly in a giant switch statement?)

1 Like

Huh, you fixed my problem. the line of script I was trying to change actually works perfectly, but that line that had ā€˜Var’ in it was stopping it from doing so. removed it and now what I originally had works!
In regards to the 64 different combinations, it’s only responding to 1 set of X,Y,Z values for each side, unfortunately I’m having to go through and find them all manually…
Thanks heaps, the game couldn’t function without this!