Rotation equals -90 instead 270 degrees

Hello mates,

I’m begginer with Unity and I tried to apply a rotation to a gameObject (0f, 270f, 0f).

else if (rand == 3)
        {
            currentFace = "Left";
            gameObject.transform.rotation = Quaternion.Euler(0f, 270f, 0f);
        }

but when i start the game the rotation is set to -90°.
I try many way to apply rotation (Quaternion, Euler.Angles) and impossible to have 270° on my inspector !
Can you help me to fix this ?
Thank you.

public string currentFace;

    private void Awake()
    {
        int rand = Random.Range(0, 4);

        if (rand == 0)
        {
            currentFace = "Front";
            this.transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
        }
        else if (rand == 1)
        {
            currentFace = "Right";
            this.transform.localRotation = Quaternion.Euler(0f, 90f, 0f);
        }
        else if (rand == 2)
        {
            currentFace = "Back";
            this.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
        }
        else if (rand == 3)
        {
            currentFace = "Left";
            gameObject.transform.rotation = Quaternion.Euler(0f, 270f, 0f);
        }
    }

Hi @kiltropp

This happens because Unity rotations use internally quaternions, euler rotations you see in inspector are just interpretation of that rotation.

For this reason I guess -90 creates the same rotation as 270. So you don’t see 270 even though your object points to left. You can also set euler values like 720 and 1080, those two will both point forward… so you have stored “revolutions count” for that object, but when you set heading of your object using unity’s quaternion class, it does only rotate around virtual sphere once.

Somebody else can explain why, I only know it happens…

1 Like

-90 is the same rotation as 270 - there’s no actual problem here. It doesn’t matter what the inspector displays.

An explanation: It’s important to realize that the Euler angles are not how Unity stores rotations. It uses Quaternions, which have four numbers from -1 to 1, which have little apparent relation to the Euler angle numbers. It uses these to avoid gimbal lock which you can google for more information.

So when you call:

transform.localRotation = Quaternion.Euler(0f, 270f, 0f);

It converts the (0,270,0) into a Quaternion representing that rotation. The number “270” no longer exists at this point. Now, the inspector has to display the rotation, but it doesn’t have that 270 number. And that particular Quaternion has an infinite number of possible permutations - (0,-90,0), (0,270,0), (0,630,0), (360,-90,0), to name just a few. So the Quaternion-to-Euler algorithm has to just pick one and go with it, and in this case it’s decided to go with -90.

(Now, if you type “270” directly into the inspector, the Inspector itself saves that number, so that it can display that number as long as the rotation doesn’t get changed elsewhere. This value can’t be set or changed in code and doesn’t even exist in runtime.)

There are a lot of caveats to Euler angles, and this problem gets far worse when you rotate on multiple axes. Check out this article for more information. The tldr is that Euler angles suck for most applications.

2 Likes

Other than being displayed differently than you expect (-90 instead of 270 degrees), do you observe any problems? If you set the rotation to 270, the object should look correct, evven if it’s rotational value is displayed different from what you expect, doesn’t it?

Is there a tangible Problem you are experiencing?

1 Like

I would try some shit and I’ll see if this case give me some problems.

Thank you guys !

did you solve it same problem here

Set the starting rotation of that gameObject, in the Inspector, to 180,180,180. It will display all values from 0-360.

The Inspector tries to show you “nice” values, as near as possible to the ones you entered before you pressed Play. If you start at 0’s, it assumes you like to see -180 to 180. This makes absolutely no difference in how the game runs. Even if you see 270, it turns out your program will see -90.

Unless, of course, their scrip does something like

if (direction < -80)

which I believe is the source for many of these questions, i.e. insufficient trig knowledge that angles are periodic.

Got it sorted thanks guys

270 is not working… same for 90, even if i set it to 90.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;

public class meinStruggle : MonoBehaviour
{

    int blocksOn = 1;
    int blockSpawned = 0;
    int currentPosition = 4;
    GameObject activeBlock;
    int blockMovementSpeed = 100;
    private float decay;
    public float blockRotationSpeed = 100f;
    float smooth = 5.0f;



    Vector3[] blockPosition = new[] {
        new Vector3(10f, 10f, 0f),
        new Vector3(8.6602f, 10f, -5f),
        new Vector3(4.999999f, 10f, -8.660254f),
        new Vector3(-9.536743f, 10f, -10f),
        new Vector3(-5.000001f, 10f, -8.660254f),
        new Vector3(-8.660256f, 10f, -5f),
        new Vector3(-10f, 10f, 0f),
        new Vector3(-8.660258f, 10f, 5.000001f),
        new Vector3(-5.000005f, 10f, 8.660255f),
        new Vector3(-4.768372f, 10f, 10f),
        new Vector3(4.999996f, 10f, 8.660257f),
        new Vector3(8.660252f, 10f, 5.000003f),
    };


    Vector3[] blockRotation = new[] {
        new Vector3(90, 0,  0),
        new Vector3(90,  30, 0),
        new Vector3(90,  60, 0),
        new Vector3(90,  90, 0),
        new Vector3(90,  120, 0),
        new Vector3(90,  150, 0),
        new Vector3(90,  180, 0),
        new Vector3(90,  210, 0),
        new Vector3(90,  240, 0),
        new Vector3(90,  270, 0),
        new Vector3(90,  300, 0),
        new Vector3(90,  330, 0)


    };

    public GameObject block;


    // Start is called before the first frame update
    void Start()
    {








    }

    // Update is called once per frame
    void Update()
    {

        Reset();

        if (blocksOn == 1 && blockSpawned == 0)
        {



            activeBlock = (GameObject)Instantiate(block, new Vector3(blockPosition[currentPosition][0], 30, blockPosition[currentPosition][2]), Quaternion.Euler(blockRotation[currentPosition][0], blockRotation[currentPosition][1], blockRotation[currentPosition][2]));

            print(" BP is : " + blockPosition[currentPosition][0]);

            blockSpawned = 1;

           



           


        }



        // keyboard input
        float horizontalValue = Input.GetAxis("Horizontal");
        float verticalValue = Input.GetAxis("Vertical");

        if (horizontalValue < 0 && decay == 0)
        {
            currentPosition = currentPosition - 1;
            //if current position is negative out of range going left, loop back to 11
            if (currentPosition == -1)
            {
                currentPosition = 11;
            }
            // if current position is positive out of range going right, loop back to 0
            if (currentPosition > 11)
            {
                currentPosition = 0;
            }
            decay = 0.25f;
            print("Left");
            //  StartCoroutine(RotateMe(Vector3.right * -30, decay));
            // activeBlock.transform.Rotate(blockRotation[currentPosition],Space.World);

            //activeBlock.transform.rotation = Quaternion.Euler(90, 0, -1 * (currentPosition*30));

            if(currentPosition == 9) {
            }
            activeBlock.transform.rotation = new Quaternion(-0.5f, 0.5f, -0.5f, -0.5f);


            //activeBlock.transform.Rotate(0,-30,0   , Space.World);

            //Quaternion.Euler(

        }

        if (horizontalValue > 0 && decay == 0)
        {
            currentPosition = currentPosition + 1;
            //if current position is negative out of range going left, loop back to 11
            if (currentPosition == -1)
            {
                currentPosition = 11;
            }
            // if current position is positive out of range going right, loop back to 0
            if (currentPosition > 11)
            {
                currentPosition = 0;
            }
            decay = 0.25f;
            print("Right");
            //  StartCoroutine(RotateMe(Vector3.right * 30, decay));
           // activeBlock.transform.Rotate(blockRotation[currentPosition],Space.World);

        }

        if (verticalValue < 0)
        {
            //DROP THE FUCKING BLOCK
            print("Down");
        }

        if (verticalValue > 0)
        {
            print("Up");
        }





        // calc move
        Vector3 newPos = Vector3.MoveTowards(activeBlock.transform.position, new Vector3(blockPosition[currentPosition][0], blockPosition[currentPosition][1], blockPosition[currentPosition][2]), blockMovementSpeed * Time.deltaTime);
        //move
        activeBlock.transform.position = newPos;







        //calc tween rotation


        //activeBlock.transform.rotation = Quaternion.LookRotation(blockRotation[currentPosition]);

        // activeBlock.transform.rotation = new Vector3(blockPosition[currentPosition][0], blockPosition[currentPosition][1], blockPosition[currentPosition][2]);

       







        print("CP : " + currentPosition);




    }






    private void Reset()
    {
        if (decay > 0)
        {
            decay -= Time.deltaTime;
        }
        if (decay < 0)
        {
            decay = 0;

        }


    }






    IEnumerator RotateMe(Vector3 byAngles, float inTime)
    {
        var fromAngle = activeBlock.transform.rotation;
        var toAngle = Quaternion.Euler(activeBlock.transform.eulerAngles + byAngles);
        for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
        {
            activeBlock.transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t);
            yield return null;
        }
    }




}

What exactly isn’t working?