Rotates a GameObject to original rotation.

Hello, if anyone can help me, I want to rotate a GameObject normally(already done), and when a bool is false, it returns to the origin in the same direction and speed. Like the Leica RTC360.
Grateful, in advance.

public class Scan_Movement : MonoBehaviour
{
    public Transform scan_main;
    public Transform scan_eye;
    public bool scanning;
    private Vector3 origin;

    public float omega = 0;
    private float omegaMax = 3000.0f;
    private float omegaRate = 75.0f;

    // Start is called before the first frame update
    void Start()
    {
        origin = scan_main.localEulerAngles;

    }
    public float rotationSpeed;

    void FixedUpdate()
    {
        // Rotates the object.
        if (scanning)
        {
            rotationSpeed = 50f;
            if (omega < omegaMax)
            {
                omega += omegaRate;
                if (omega > omegaMax)
                    omega = omegaMax;
            }
            scan_eye.Rotate(omega * Time.deltaTime * Vector3.up);

            // Here.
            scan_main.Rotate(new Vector3(0f, 0f, rotationSpeed * Time.deltaTime));
        }
        // Goes to the origin.
        else if (scanning == false)
        {
            if (omega > 0f)
            {
                omega -= (omegaRate + 25);
                if (omega < 0f)
                {
                    omega = 0f;
                }
            }
            scan_eye.Rotate(omega * Time.deltaTime * Vector3.up);

            // Here.
            if (scan_main.localEulerAngles.z != 0f)
            {
                scan_main.eulerAngles = Vector3.RotateTowards(scan_main.eulerAngles, origin, rotationSpeed, 0f) * Time.deltaTime;

            }
        }
        print(scan_main.localEulerAngles.z);
    }

}

Best way is to keep two rotations for each end and then use Quaternion.Lerp() or Quaternion.Slerp() to go smoothly between them.

I just did this yesterday with my block destroyer/creator.

Code is at:

Upon restoring blocks, line 160 tweens from the one rotation to the other and line 163 assigns it into the flying piece.

Be sure you understand how Lerp/Slerp works and the function of the third “alpha” term.

2 Likes

Hello Kurt Dekker, thank you very much for the help, it is working now!!!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Scan_Movement : MonoBehaviour
{
    public Transform scan_main;
    public Transform scan_eye;

    private Quaternion bodyOrigin;
    private Quaternion eyeOrigin;

    public bool scanning;
    bool end = false;


    private float omega = 0;
    private float omegaMax = 3000.0f;
    private float omegaRate = 40.0f;

    public float rotationSpeed = 30f;
    private float currentSpeed;

    // Start is called before the first frame update
    void Start()
    {
        // Gets the rotations at origin
        bodyOrigin = scan_main.rotation;
        eyeOrigin = scan_eye.rotation;

        currentSpeed = rotationSpeed;
    }

    void FixedUpdate()
    {
        ScanEye();
        ScanMain();
    }

    void ScanEye()
    {
        // Start scanning.
        if (scanning && end == false)
        {
            // Increases the object rotation.
            if (omega < omegaMax)
            {
                omega += omegaRate;
                if (omega > omegaMax)
                    omega = omegaMax;
            }
            scan_eye.Rotate(omega * Time.deltaTime * Vector3.up);
        }
        else
        {

            // Continues the rotation to a certain amount.
            scan_eye.Rotate(omega * Time.deltaTime * Vector3.up);

            if (end)
            {

                omega -= 1;
                if (scan_eye.localEulerAngles.y > 270f)
                {
                    omega = 0f;
                }
                if (omega < 0f)
                {
                    omega = 0f;
                }
                // Goes to the origin.
                if (omega == 0f)
                {

                    ///////// Copyright: Kurt Dekker (https://github.com/kurtdekker/makegeo/blob/master/makegeo/Assets/BuildingBlocks/BuildingBlocks.cs)
                    Quaternion currentRotation = scan_eye.rotation;

                    float ArcInterval = Random.Range(0.5f, 0.7f);
                    float time = 0;
                    float fraction;

                    time += Time.deltaTime;
                    fraction = time / ArcInterval;

                    float angle = fraction * Mathf.PI;
                    float cosineTween = (-Mathf.Cos(angle) + 1) / 2;
                    float sineTween = Mathf.Sin(angle) / 2;

                    Quaternion rotation = Quaternion.Slerp(currentRotation, eyeOrigin, sineTween);
                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                   
                    scan_eye.rotation = rotation;
                    if (scan_eye.rotation == rotation)
                        end = false;
                }

            }
        }

    }

    void ScanMain()
    {
        // Start scanning.
        if (scanning && omega == omegaMax)
        {
            // Rotates the object.
            rotationSpeed = currentSpeed;
            scan_main.Rotate(new Vector3(0f, 0f, rotationSpeed * Time.deltaTime));
        }
        else
        {
            // Goes to the origin.
            if (scan_main.localEulerAngles.z > 330f)
            {
                ///////// Copyright: Kurt Dekker (https://github.com/kurtdekker/makegeo/blob/master/makegeo/Assets/BuildingBlocks/BuildingBlocks.cs)
                Quaternion currentRotation = scan_main.rotation;

                float ArcInterval = Random.Range(0.5f, 0.7f);
                float time = 0;
                float fraction;
                time += Time.deltaTime;
                fraction = time / ArcInterval;

                float angle = fraction * Mathf.PI;
                float cosineTween = (-Mathf.Cos(angle) + 1) / 2;
                float sineTween = Mathf.Sin(angle) / 2;

                Quaternion rotation = Quaternion.Slerp( currentRotation, bodyOrigin, sineTween);
                /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
               
                scan_main.rotation = rotation;
                rotationSpeed = 0f;
            }
            else if (scan_main.localEulerAngles.z <= 330f && scan_main.localEulerAngles.z > 0.01f)
            {
                // Continues the rotation to a certain amount.
                scan_main.Rotate(new Vector3(0f, 0f, rotationSpeed * Time.deltaTime));
            }
           
            // Stops the eye rotation.
            if (scan_main.localEulerAngles.z >= 359.85f)
                end = true;
        }
    }

}
1 Like