Search code examples
c#unity-game-enginerotationsmoothing

Smooth with mouse object rotation


I use this line to rotate object using mouse:

this._obj.transform.Rotate(new Vector3(0, -Input.GetAxis("Mouse X"), 0), Time.unscaledDeltaTime * this.speed);

But I would like to smooth this out, with some delay like in Sketchfab when you use the mouse to rotate the object, just not sure how to do this properly. I'm not really familiar with the Quaternions that much.

Can someone give me some advice on how I just approach this?


Solution

  • Its a bit complicated just to rotate it. Here's a working version of what you want:

        public Vector3 hiddenRotation;
    
    void Start()
    {
        hiddenRotation = this.transform.rotation.eulerAngles;
    }
    
    void Update()
    {
        float sensitivity = 5.0f;
        //Get rotation vector
        Vector3 rot = new Vector3(0, -Input.GetAxis("Mouse X"), 0) * sensitivity;
        //Add rotation to hidden internal rotation
        hiddenRotation += rot;
    
        //Specify speed at which the cube rotates.
        float speed = 50.0f;
        float step = speed * Time.deltaTime;
    
        //Set new rotation (changing this function may allow for smoother motion, like lower step as it approaches the new rotation
        this.transform.rotation = Quaternion.RotateTowards(this.transform.rotation, Quaternion.Euler(hiddenRotation), step);
    
    }
    

    Which uses a hidden rotation and gets the transform.rotation to approach it.