Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Rotate object by 90 degrees over time.

Discussion in 'Scripting' started by unity_nVdYtceNQmOPDw, Nov 17, 2019.

  1. unity_nVdYtceNQmOPDw

    unity_nVdYtceNQmOPDw

    Joined:
    Jun 12, 2018
    Posts:
    15
    Hi,i'd like to rotate an object by 90 degrees over a small amount of time so it would create a nice rotation effect.
    For example,if i click on the right side of the screen the object would rotate by -90 degrees with a visible rotating effect,because if i simply write: transform.Rotate(0, 0, -90); then it snaps to the new rotation.


    void Update()
    {
    if (Input.GetMouseButtonDown(0) && Input.mousePosition.x > Screen.width / 2)
    {
    //rotate by -90 degrees
    } else if(Input.GetMouseButtonDown(0) && Input.mousePosition.x < Screen.width / 2)
    {
    //rotate by +90 degrees
    }
    }
    Thanks in advance.
     
  2. Yousse-fDev

    Yousse-fDev

    Joined:
    Oct 13, 2019
    Posts:
    5
    transform.Rotate(Vector3.up * speedVariable * Time.deltaTime);
    Play around with Vector3.DIRECTION to see what you want
     
    unity_nVdYtceNQmOPDw likes this.
  3. unity_nVdYtceNQmOPDw

    unity_nVdYtceNQmOPDw

    Joined:
    Jun 12, 2018
    Posts:
    15
    Thanks,but i want it to roll,i forgot to add that,so i would like it to rotate around the z axis.
    I dont really get quaternions just yet,but it might be the answer.
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    There are a few ways you could do it. You could use a coroutine and add 90 degrees to the current rotation for example. A simple way would be like so;

    Code (CSharp):
    1. [SerializeField]
    2. private float _rotationSpeed = 5f;
    3.  
    4. private Quaternion _targetRot;
    5.  
    6. private void Awake()
    7. {
    8.     _targetRot = transform.rotation;
    9. }
    10.  
    11. private void Update()
    12. {
    13.     if (Input.GetMouseButtonDown(0) && Input.mousePosition.x > Screen.width / 2)
    14.     {
    15.         _targetRot = Quaternion.AngleAxis(-90, transform.forward);
    16.         //If you want to rotate each time the mouse is clicked
    17.         //_targetRot *= Quaternion.AngleAxis(-90, transform.forward);
    18.     }
    19.     else if (Input.GetMouseButtonDown(0) && Input.mousePosition.x < Screen.width / 2)
    20.     {
    21.         _targetRot = Quaternion.AngleAxis(90, transform.forward);
    22.         //If you want to rotate each time the mouse is clicked
    23.         //_targetRot *= Quaternion.AngleAxis(90, transform.forward);
    24.     }
    25.  
    26.     transform.rotation = Quaternion.Lerp(transform.rotation, _targetRot, _rotationSpeed * Time.deltaTime);
    27. }
     
  5. unity_nVdYtceNQmOPDw

    unity_nVdYtceNQmOPDw

    Joined:
    Jun 12, 2018
    Posts:
    15
    Thank you!I'm going to try this out tomorrow!