0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateObjects : MonoBehaviour
{
    public GameObject[] objectsToRotate;
    public Transform to;
    public float speed = 0.1f;
    public Vector3 spinDirection;
    public bool useMouse = false;
    public bool useQuaternion = false;
    public bool nonstopSpin = false;

    float x;

    // Use this for initialization
    void Start()
    {
        if (objectsToRotate.Length > 0)
        {
            if (useQuaternion == true)
            {
                for (int i = 0; i < objectsToRotate.Length; i++)
                {
                    objectsToRotate[i].transform.rotation = Quaternion.Lerp(objectsToRotate[i].transform.rotation, to.rotation, Time.time * speed);
                }
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (objectsToRotate.Length > 0)
        {
            for (int i = 0; i < objectsToRotate.Length; i++)
            {
                x += Time.deltaTime * speed;
                objectsToRotate[i].transform.rotation = Quaternion.Euler(x, 0, 0);

                if (useMouse == true)
                {
                    if (Input.GetMouseButton(0))
                    {
                        Rotate(i);
                    }
                }
                else
                {
                    Rotate(i);
                }
            }
        }
    }

    private void Rotate(int i)
    {
        if (useQuaternion == true)
        {
            objectsToRotate[i].transform.rotation = Quaternion.Lerp(objectsToRotate[i].transform.rotation, to.rotation, Time.time * speed);
        }

        if (nonstopSpin == true)
        {
            objectsToRotate[i].transform.Rotate(0, 1, 0);
        }
    }
}

In the Update for testing I'm using Quaternion.Euler to spin the object. In the bottom I'm using transform.Rotate.

In both cases I'm getting the same results the object is spinning around it self.

Then what is the difference between them and what should I choose when I want to spin an object ? And maybe there are more way to make an object to spin around it self. But I wonder what is the difference.

\$\endgroup\$
1
\$\begingroup\$

Quaternion.Euler generates a Quaternion that represents the orientation or relative rotation specified by the Euler/Tait-Bryan angles you provide as inputs.

Transform.Rotate rotates a transform by an incremental amount, specified by the Euler/Tait-Bryan angles you provide as inputs.

Internally, Transform.Rotate might call Quaternion.Euler or something like it to compute the rotation from the angles. For instance, it might look like this:

public void Rotate(float x, float y, float z, Space relativeTo = Space.Self) {
    Quaternion increment = Quaternion.Euler(x, y, z);

    if(relativeTo == Space.Self)
        this.rotation = this.rotation * increment;
    else
        this.rotation = increment * this.rotation;
}

So when you just want to rotate an object, you can use either one interchangeably.

Transform.Rotate provides a little convenience for doing this all in one line, with a handy Space selection so you don't have to remember which multiplication order corresponds to local vs world.

But sometimes you don't want to rotate an object right away. Sometimes you just want to transform some vectors. Or you want to cache a rotation rather than apply it to an object immediately. Or you want to do some math to compare, combine & blend orientations before applying them. For those cases, it's handy to be able to create a free-floating Quaternion to work with the rotation, rather than spawn a bunch of dummy objects to rotate whenever you want to do some rotation math. That's where things like Quaternion.Euler or .AngleAxis come in handy.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.