Search Unity

Make a 360° rotation on demand

Discussion in 'Scripting' started by HectorUD, Sep 22, 2018.

  1. HectorUD

    HectorUD

    Joined:
    Sep 9, 2018
    Posts:
    4
    Hi I'm having trouble with my script when I try to make the rotation faster. With this script I want, when I click on 'a', to rotate an object by 360°, and reverse direction when I click on 'z' , with this script everything is working well as long as the rotation is not too fast. But I need to make the rotation faster. Does anyone have a solution for this issue ? It would help me a lot.
    Thank you guys in advance !

    The code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RotationScript : MonoBehaviour
    6. {
    7.  
    8.     private Quaternion Quaternion_Rotate_From;
    9.     private Quaternion Quaternion_Rotate_To;
    10.     private int pos1;
    11.     private int pos2;
    12.     static float T = 0.0f;
    13.  
    14.     void Start()
    15.     {
    16.         pos1 = 0;
    17.         pos2 = 0;
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (Input.GetKeyDown("a"))
    23.         {
    24.             pos1 = 0;
    25.             pos2 = 360;
    26.             T = 0.0f;
    27.             Quaternion_Rotate_From = Quaternion.Euler(80, 0, 0);
    28.         }
    29.  
    30.         if (Input.GetKeyDown("z"))
    31.         {
    32.             pos1 = 0;
    33.             pos2 = -360;
    34.             T = 0.0f;
    35.             Quaternion_Rotate_From = Quaternion.Euler(-80, 0, 0);
    36.         }
    37.         T += Time.deltaTime * 0.5f;
    38.         Quaternion_Rotate_From = transform.rotation;
    39.         Quaternion_Rotate_To = Quaternion.Euler(Mathf.Lerp(pos1, pos2, T), 0, 0);
    40.         transform.rotation = Quaternion.Lerp(Quaternion_Rotate_From, Quaternion_Rotate_To, Time.deltaTime * 5);
    41.  
    42.     }
    43. }