Door rotating the wrong way around.

My door is supposed to open for a couple seconds then close. It opens fine but when it has to close it doesn’t rotate backwards it keeps going a full 360.

 case DoorState.CLOSED:
                {
                    Debug.Log(Quaternion.Angle(transform.rotation, Quaternion.Euler(0, 0, 0)));
                    if (Quaternion.Angle(transform.rotation, Quaternion.Euler(0, 0, 0)) > 1)
                    {
                        Debug.Log("BACKING UP");
                        transform.RotateAround(pivot.position, pivot.transform.up, Time.deltaTime * speed);
                        openTimer = openTime;
                    }
                    break;
                }
            case DoorState.BANGING:
                {
                    break;
                }
            case DoorState.OPEN_IN:
                {
                    if (Quaternion.Angle(transform.rotation, Quaternion.Euler(0, 90, 0)) > 1)
                    {
                        transform.RotateAround(pivot.position, pivot.transform.up, Time.deltaTime * speed);
                        openTimer = openTime;
                    }
                    else if(openTimer < 0)
                    {
                        state = DoorState.CLOSED;
                    } else
                    {
                        openTimer -= Time.deltaTime;
                    }
                    break;

                }

Hi!

It’s because transform.RotateAround function take 3rd variable as an angle, not step, what means that you should specify if you want to rotate in specific direction. Simply change positive speed to -speed when you want to rotate it back.

You also can use animation for something like that and just play it when necessary, but that’s up to you.


Hope that helps.