D
D
djtoryx2016-10-28 17:12:48
Unity
djtoryx, 2016-10-28 17:12:48

How to determine the "distance" of an angle to zero?

I have an object that all the time tends to set the angle values ​​(0, 0, 0). And the speed of reaching zero depends on the current angle.
The problem is that when I want to rotate an object to the right, I sort of subtract from the current player. But the value does not become negative, but is subtracted from 360 degrees.
Let's say if I want "0 - 25" then it will not be equal to -25, but it will be 360 ​​- 25 = 335.

transform.eulerAngles = new Vector3(Mathf.MoveTowardsAngle(x, 0, Mathf.Abs(x) * _kickBack * Time.deltaTime), Mathf.MoveTowardsAngle(y, 0, Mathf.Abs(y) * _kickBack * Time.deltaTime));

Mathf.Abs(x) должно являться расстоянием до 0.
А теперь вопрос!
Есть ли встроенные методы для определения АДЕКВАТНОЙ разницы значения угла до 0?
Или придётся писать какой то свой метод?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
djtoryx, 2016-10-28
@djtoryx

Mathf.Repeat(current + 180, 360) - 180

D
Daniil Basmanov, 2016-10-28
@BasmanovDaniil

MoveTowardsAngle specifically changes negative angles to positive ones, if you don't want this behavior, then use the normal MoveTowards.

public static float MoveTowardsAngle(float current, float target, float maxDelta)
{
    float num = Mathf.DeltaAngle(current, target);
    if (-(double) maxDelta < (double) num && (double) num < (double) maxDelta)
    return target;
    target = current + num;
    return Mathf.MoveTowards(current, target, maxDelta);
}

public static float DeltaAngle(float current, float target)
{
    float num = Mathf.Repeat(target - current, 360f);
    if ((double) num > 180.0)
    num -= 360f;
    return num;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question