J
J
Jamal Aklambekov2020-08-19 20:57:08
C++ / C#
Jamal Aklambekov, 2020-08-19 20:57:08

How to make uniform Quaternion.Lerp?

Is it possible to make Quaternion.Lerp smoother and more uniform? So that the speed at the edges of the values ​​\u200b\u200bis at least approximately equal to the middle?

Let's say I need to rotate an object from the current angle to the given one. But the turn should be smooth. And he is at the beginning and at the end barely weaving.

[SerializeField] private float _rotationSpeed;
[SerializeField] private Quaternion _targetRotation;

private Quaternion _targetRotation;

private Quaternion _maxRotation;
private Quaternion _minRotation;

 private void Start()
    {
        _targetRotation = _maxRotation;

        StartCoroutine(LoopRotation());
    }

private void Rotate()
    {
        transform.rotation = Quaternion.LerpUnclamped(transform.rotation, _targetRotation, _rotationSpeed * Time.deltaTime);
    }

private IEnumerator LoopRotation()
    {
        while (true)
        {
            Rotate();
            //просто изменение угла, к которому нужно стремиться
            if (transform.rotation.z >= _maxRotation.z - 0.02f)
                _targetRotation = _minRotation;
            else if (transform.rotation.z <= _minRotation.z + 0.02f)
                _targetRotation = _maxRotation;
            yield return null;
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2020-08-19
@GavriKos

Coroutine does not rotate anything. Spins your code running in a coroutine. So it should be applied to the question - for sure there is an error in it and it is non-linear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question