Search Unity

Rotates a GameObject to original rotation.

Discussion in 'Scripting' started by Nicolas_Granese, Sep 24, 2021.

  1. Nicolas_Granese

    Nicolas_Granese

    Joined:
    Mar 25, 2020
    Posts:
    5
    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.

    Code (CSharp):
    1. public class Scan_Movement : MonoBehaviour
    2. {
    3.     public Transform scan_main;
    4.     public Transform scan_eye;
    5.     public bool scanning;
    6.     private Vector3 origin;
    7.  
    8.     public float omega = 0;
    9.     private float omegaMax = 3000.0f;
    10.     private float omegaRate = 75.0f;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         origin = scan_main.localEulerAngles;
    16.  
    17.     }
    18.     public float rotationSpeed;
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         // Rotates the object.
    23.         if (scanning)
    24.         {
    25.             rotationSpeed = 50f;
    26.             if (omega < omegaMax)
    27.             {
    28.                 omega += omegaRate;
    29.                 if (omega > omegaMax)
    30.                     omega = omegaMax;
    31.             }
    32.             scan_eye.Rotate(omega * Time.deltaTime * Vector3.up);
    33.  
    34.             // Here.
    35.             scan_main.Rotate(new Vector3(0f, 0f, rotationSpeed * Time.deltaTime));
    36.         }
    37.         // Goes to the origin.
    38.         else if (scanning == false)
    39.         {
    40.             if (omega > 0f)
    41.             {
    42.                 omega -= (omegaRate + 25);
    43.                 if (omega < 0f)
    44.                 {
    45.                     omega = 0f;
    46.                 }
    47.             }
    48.             scan_eye.Rotate(omega * Time.deltaTime * Vector3.up);
    49.  
    50.             // Here.
    51.             if (scan_main.localEulerAngles.z != 0f)
    52.             {
    53.                 scan_main.eulerAngles = Vector3.RotateTowards(scan_main.eulerAngles, origin, rotationSpeed, 0f) * Time.deltaTime;
    54.  
    55.             }
    56.         }
    57.         print(scan_main.localEulerAngles.z);
    58.     }
    59.  
    60. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    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:

    https://github.com/kurtdekker/makegeo/blob/master/makegeo/Assets/BuildingBlocks/BuildingBlocks.cs

    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.
     
    Schneider21 and Nicolas_Granese like this.
  3. Nicolas_Granese

    Nicolas_Granese

    Joined:
    Mar 25, 2020
    Posts:
    5
    Hello Kurt Dekker, thank you very much for the help, it is working now!!!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Scan_Movement : MonoBehaviour
    6. {
    7.     public Transform scan_main;
    8.     public Transform scan_eye;
    9.  
    10.     private Quaternion bodyOrigin;
    11.     private Quaternion eyeOrigin;
    12.  
    13.     public bool scanning;
    14.     bool end = false;
    15.  
    16.  
    17.     private float omega = 0;
    18.     private float omegaMax = 3000.0f;
    19.     private float omegaRate = 40.0f;
    20.  
    21.     public float rotationSpeed = 30f;
    22.     private float currentSpeed;
    23.  
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.         // Gets the rotations at origin
    28.         bodyOrigin = scan_main.rotation;
    29.         eyeOrigin = scan_eye.rotation;
    30.  
    31.         currentSpeed = rotationSpeed;
    32.     }
    33.  
    34.     void FixedUpdate()
    35.     {
    36.         ScanEye();
    37.         ScanMain();
    38.     }
    39.  
    40.     void ScanEye()
    41.     {
    42.         // Start scanning.
    43.         if (scanning && end == false)
    44.         {
    45.             // Increases the object rotation.
    46.             if (omega < omegaMax)
    47.             {
    48.                 omega += omegaRate;
    49.                 if (omega > omegaMax)
    50.                     omega = omegaMax;
    51.             }
    52.             scan_eye.Rotate(omega * Time.deltaTime * Vector3.up);
    53.         }
    54.         else
    55.         {
    56.  
    57.             // Continues the rotation to a certain amount.
    58.             scan_eye.Rotate(omega * Time.deltaTime * Vector3.up);
    59.  
    60.             if (end)
    61.             {
    62.  
    63.                 omega -= 1;
    64.                 if (scan_eye.localEulerAngles.y > 270f)
    65.                 {
    66.                     omega = 0f;
    67.                 }
    68.                 if (omega < 0f)
    69.                 {
    70.                     omega = 0f;
    71.                 }
    72.                 // Goes to the origin.
    73.                 if (omega == 0f)
    74.                 {
    75.  
    76.                     ///////// Copyright: Kurt Dekker (https://github.com/kurtdekker/makegeo/blob/master/makegeo/Assets/BuildingBlocks/BuildingBlocks.cs)
    77.                     Quaternion currentRotation = scan_eye.rotation;
    78.  
    79.                     float ArcInterval = Random.Range(0.5f, 0.7f);
    80.                     float time = 0;
    81.                     float fraction;
    82.  
    83.                     time += Time.deltaTime;
    84.                     fraction = time / ArcInterval;
    85.  
    86.                     float angle = fraction * Mathf.PI;
    87.                     float cosineTween = (-Mathf.Cos(angle) + 1) / 2;
    88.                     float sineTween = Mathf.Sin(angle) / 2;
    89.  
    90.                     Quaternion rotation = Quaternion.Slerp(currentRotation, eyeOrigin, sineTween);
    91.                     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    92.                    
    93.                     scan_eye.rotation = rotation;
    94.                     if (scan_eye.rotation == rotation)
    95.                         end = false;
    96.                 }
    97.  
    98.             }
    99.         }
    100.  
    101.     }
    102.  
    103.     void ScanMain()
    104.     {
    105.         // Start scanning.
    106.         if (scanning && omega == omegaMax)
    107.         {
    108.             // Rotates the object.
    109.             rotationSpeed = currentSpeed;
    110.             scan_main.Rotate(new Vector3(0f, 0f, rotationSpeed * Time.deltaTime));
    111.         }
    112.         else
    113.         {
    114.             // Goes to the origin.
    115.             if (scan_main.localEulerAngles.z > 330f)
    116.             {
    117.                 ///////// Copyright: Kurt Dekker (https://github.com/kurtdekker/makegeo/blob/master/makegeo/Assets/BuildingBlocks/BuildingBlocks.cs)
    118.                 Quaternion currentRotation = scan_main.rotation;
    119.  
    120.                 float ArcInterval = Random.Range(0.5f, 0.7f);
    121.                 float time = 0;
    122.                 float fraction;
    123.                 time += Time.deltaTime;
    124.                 fraction = time / ArcInterval;
    125.  
    126.                 float angle = fraction * Mathf.PI;
    127.                 float cosineTween = (-Mathf.Cos(angle) + 1) / 2;
    128.                 float sineTween = Mathf.Sin(angle) / 2;
    129.  
    130.                 Quaternion rotation = Quaternion.Slerp( currentRotation, bodyOrigin, sineTween);
    131.                 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    132.                
    133.                 scan_main.rotation = rotation;
    134.                 rotationSpeed = 0f;
    135.             }
    136.             else if (scan_main.localEulerAngles.z <= 330f && scan_main.localEulerAngles.z > 0.01f)
    137.             {
    138.                 // Continues the rotation to a certain amount.
    139.                 scan_main.Rotate(new Vector3(0f, 0f, rotationSpeed * Time.deltaTime));
    140.             }
    141.            
    142.             // Stops the eye rotation.
    143.             if (scan_main.localEulerAngles.z >= 359.85f)
    144.                 end = true;
    145.         }
    146.     }
    147.  
    148. }
    149.  
     
    Last edited: Sep 29, 2021
    Kurt-Dekker likes this.