Rotate object by 90 degrees over time.

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.

transform.Rotate(Vector3.up * speedVariable * Time.deltaTime);
Play around with Vector3.DIRECTION to see what you want

1 Like

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.

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;

[SerializeField]
private float _rotationSpeed = 5f;

private Quaternion _targetRot;

private void Awake()
{
    _targetRot = transform.rotation;
}

private void Update()
{
    if (Input.GetMouseButtonDown(0) && Input.mousePosition.x > Screen.width / 2)
    {
        _targetRot = Quaternion.AngleAxis(-90, transform.forward);
        //If you want to rotate each time the mouse is clicked
        //_targetRot *= Quaternion.AngleAxis(-90, transform.forward);
    }
    else if (Input.GetMouseButtonDown(0) && Input.mousePosition.x < Screen.width / 2)
    {
        _targetRot = Quaternion.AngleAxis(90, transform.forward);
        //If you want to rotate each time the mouse is clicked
        //_targetRot *= Quaternion.AngleAxis(90, transform.forward);
    }

    transform.rotation = Quaternion.Lerp(transform.rotation, _targetRot, _rotationSpeed * Time.deltaTime);
}
2 Likes

Thank you!I’m going to try this out tomorrow!