Unity | Rotate the gameObject by specified angle (0~360 degree)

19,695

Solution 1

Use RotateAround.

// Rotate around world y.
transform.RotateAround(transform.position, Vector3.up, angle);

// Rotate around local y.
transform.RotateAround(transform.position, transform.up, angle);

You may found other useful stuff in Transform documentation anyway.

Solution 2

transform.eulerAngles = new Vector3(90, 0, 0);

Rotates your gameobject to 90 degrees in x axis.

Or you can rotate smoothly with

Vector3 destination = new Vector3(90,0,0);
transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, 
                                     destination, 
                                     Time.deltaTime);
Share:
19,695
홍의숙
Author by

홍의숙

Updated on June 05, 2022

Comments

  • 홍의숙
    홍의숙 almost 2 years

    I'm struggling to rotate gameObject with joystick. The joystick send the json data included value of angle to gameObject. The gameOjbect should rotate itself when receive the Jsondata. However, i'm wondering how to rotate it by angle (0 to 360 degree) in unity because all i do know is using (Vector3) position below.

    Quaternion.LookRotation
    public static Quaternion LookRotation(Vector3 forward, 
                                          Vector3 upwards = Vector3.up);
    

    In conclusion, all i want to know is rotating the gameObject by the angle.