Main objective of this post is to give you an idea about how to work with Rotate Object by 90 Degree in Unity



Step 1 Introduction

To rotate object with clockwise or anti-clockwise in Unity you need a center of rotation and how much you want to rotate it. Counter clockwise and Anti-clockwise rotations are called as positive rotation and negative rotation respectively.

This sample Code is to explain how to rotate an object clockwise or anti-clockwise in Unity. Rotation will be perform in steps so, it will give animation effect. Additionally code will check if after rotation, object is colliding with any other object. If it is, then it will rotate again until the state in which it is colliding with no other object.


Step 2 Implementation

Video: Unity - Rotate object by 90 degree with animation:

In this video you can see rotation of an object clockwise or anti-clockwise.


2.1 Code Sample

For this code, rotation step must be between 0 to 90. It is preferable to take such rotation step, which is neither too small nor large (approximate 10).

public int rotationDirection = -1; // -1 for clockwise
     //  1 for anti-clockwise
    public int rotationStep = 10;    // should be less than 90
// All the objects with which collision will be checked
public GameObject[] objectsArray;
     private Vector3 currentRotation, targetRotation;

2.2 Find target rotation

This method should be called when rotation is required, like on touch up/ down or on specific button click.

private void rotateObject()
       currentRotation = gameObject.transform.eulerAngles;
         targetRotation.z = (currentRotation.z + (90 * rotationDirection));
StartCoroutine (objectRotationAnimation());

2.3 Start coroutine for rotation

IEnumerator objectRotationAnimation()
// add rotation step to current rotation.
         currentRotation.z += (rotationStep * rotationDirection);
         gameObject.transform.eulerAngles = currentRotation;
         yield return new WaitForSeconds (0);
         if (((int)currentRotation.z >
(int)targetRotation.z && rotationDirection < 0)  ||  // for clockwise
      ((int)currentRotation.z <  (int)targetRotation.z && rotationDirection > 0)) // for anti-clockwise
     StartCoroutine (objectRotationAnimation());
            gameObject.transform.eulerAngles = targetRotation;
            if (isCurrentObjectCollidingWithOtherObject(gameObject))
                isRotating = false;
IEnumerator rotateObjectAgain()
yield return new WaitForSeconds (0.2f);

2.4 Check if object is colliding with other object

public bool isCurrentObjectCollidingWithOtherObject
(GameObject currentObject)
foreach (GameObject objectToCheck in objectsArray)
             if (! (
                 if (currentObject.collider.bounds.Intersects
    return true;
         return false;

If you got any query related how to rotate object by 90 degree in unity 2D, comment your query below. We will try to solve it out.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Game Development Company in India.

I am skilled game developer, working in iOS and Unity3D game development since 1.5 years and loving this. Perfection and optimization is what I am focused on.

face mask Belial The Demon Headgear Pocket Staff Magic