Smooth door opening with Quaternion.Slerp issue

Hello! I’m currently trying to make my doors open smoothly in unity by using Quaternion.Slerp, only issue is that I can’t seem to get the rotation speed correct. No matter what value I use for rotation speed(float t) it immediately does the full rotation.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorLogic : MonoBehaviour
{
    public Transform character;
    public Transform doorHandle;
    public Transform pivotPoint;

    public float turnSpeed = 0.01f;

    public Quaternion currentRot;
    public Quaternion targetRot;

    void Update()
    {
        if(character.GetComponent<DoorOpenNClose>()._selection == doorHandle && Input.GetMouseButtonDown(0))
        {
            pivotPoint.rotation = Quaternion.Slerp(currentRot, targetRot, turnSpeed);
        }

    }
}

Quaternion.Slerp does not work like RotateTowards, there is no speed

All interpolating functions (such as lerp or slerp) do their thing between minimum and maximum and you’re expected to supply a value in the interval 0…1 (so called interpolant) where 0 results in minimum and 1 in maximum.

I can only advise you to read the documentation and find some videos online. There are tens of thousands of topics regarding this, doesn’t make much sense to waste everyone’s time.

Btw Quaternion.Slerp specifically clamps this interpolant, so if you’re consistently supplying a value that is greater than 1, it ends up being exactly 1, and that’s why you don’t see any change.

2 Likes

Thanks, I just ended up switching to Vector3.lerp instead.

Np, btw if you (or someone else) are interested in what the underlying math is
Vector3.LerpUnclamped is a component-wise variant of this

float lerp(float min, float max, float t) => min * (1f - t) + max * t;

and Quaternion.Slerp you can find here (along with Vector3.Slerp below it).