1
\$\begingroup\$

I'm trying to make that the object will start rotating slowly smooth increasing he rotation speed to some max speed then when reaching them ax speed to start slowly smooth decrease the speed back down until stop.

I'm trying to use SmoothStep but maybe I should use some how SmoothDamp ?

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

public class Testings : MonoBehaviour
{
    public Transform text;

    float minimum = 10.0f;
    float maximum = 20.0f;

    float duration = 5.0f;

    float startTime;

    void Start()
    {
        startTime = Time.time;
    }

    void Update()
    {
        float t = (Time.time - startTime) / duration;
        text.rotation = new Quaternion(0, Mathf.SmoothStep(minimum, maximum, t), 0, 0);
    }
}
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

You're using the wrong Quaternion method for what you're trying to do.

It looks like you're trying to smoothly damp an angle between a min and max measured in degrees.

The new Quaternion constructor does not take angles in degrees as its arguments. The first clue is that it asks for 4 parameters (x, y, z, w) not three Euler angles (x, y, z) / (pitch, yaw, roll) (what on Earth would a "w" angle rotate around?). These 4 parameters describe a point on a unit sphere in 4-dimensional space, so numbers like 20-40 make no sense here at all. And by leaving the w component at 0, you've ensured you can only get 0 or 180 degree rotations out of this expression.

What you want is Quaternion.Euler, which does accept angles in degrees:

Declaration
public static Quaternion Euler(float x, float y, float z);

Description
Returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis; applied in that order.

So your corrected code would read like this:

text.rotation = Quaternion.Euler(
                             0,
                             Mathf.SmoothStep(minimum, maximum, t),
                             0
                );

Also, note that when you compute your time this way:

float t = (Time.time - startTime) / duration;

...you will lose precision, the longer your game has been running. A better solution is to accumulate t, something like this:

// We can initialize this to a constant. 
// Now we don't even need a Start method to populate it!
float t = 0;

void Update()
{
    // Add an increment to t, 
    // proportionate to the high-precision time step since last frame.
    t += Time.deltaTime / duration;
    // ...etc.
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .