Search Unity

how to make a smooth rotation of the object?

Discussion in 'Scripting' started by Trild123787898, Dec 31, 2019.

  1. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I use this script here to rotate the object to the desired degree, how to make the object rotate smoothly?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class mehanic : MonoBehaviour
    6. {
    7.    
    8.     void FixedUpdate()
    9.     {
    10.  
    11.         if (Input.GetKey(KeyCode.E))
    12.         {
    13.            
    14.             transform.rotation = Quaternion.Euler(new Vector3(0, 0, -90));
    15.  
    16.         }
    17.     }
    18. }
    19.  
     
  2. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    You could use Quaternion.RotateTwoards
    https://docs.unity3d.com/ScriptReference/Quaternion.RotateTowards.html

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class mehanic : MonoBehaviour
    5. {
    6.  
    7.     void FixedUpdate()
    8.     {
    9.         if (Input.GetKey(KeyCode.E))
    10.         {
    11.          
    12.             transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(new Vector3(0, 0, -90), 90f * Time.deltaTime);
    13.         }
    14.     }
    15. }
    16.  
     
  3. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    thanks
     
  4. IsakHaliti

    IsakHaliti

    Joined:
    Jan 6, 2023
    Posts:
    1
    You should the new code here because that one i think is for older versions of unity:
    Code (CSharp):
    1. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, 0, -20), 90f * Time.deltaTime);